一、COMMAND WINDOW运行入门 1.1 命令行使用 1.数据的输入 A = [1,2,3;4,5,6] 2.算术运算 在命令行直接输入,ENTER结束; 3.魔方函数的运行 magic(3)
ans =
8 1 6
3 5 7
4 9 2
1.2 数值、变量和表达式 sin(3)
ans =
0.1411
变量名中不得包含空格、标点,但可以包含下划线 1.3 MATLAB默认的预定义变量 预定义变量 含义 |--ans|-计算结果的默认变量名-| | eps | 浮点相对精度 | Inf或inf 无穷大 i或j 虚数单位i=j=√-1 pi 圆周率 nan 或NAN 非数 Nargin 函数输入的变量数目 nargout 函数输入的变量数目 realmax 最大正实数 realmin 最小正实数 1.4 复数 复数的输入与和、相关函数 sd = 5+6i
sd =
5.0000 + 6.0000i
r = real(sd)
r =
5
im=imag(sd)
im =
6
a=abs(sd)
a =
7.8102
an = angle(sd)
an =
0.8761
例:复数矩阵的生成与运算 a =
2.0000 - 3.0000i 4.0000 - 7.0000i 1.0000 - 3.0000i 6.0000 - 9.0000i
b= [2+5i,3+2i;6-9i,3-5i]
b =
2.0000 + 5.0000i 3.0000 + 2.0000i 6.0000 - 9.0000i 3.0000 - 5.0000i
c = b-a
例:计算-8的立方根 a=-8;
r=a^(1/3)
r =
1.0000 + 1.7321i
例MATLAB在直接计算的过程中给出的是-8在第一象限的根,并不是-8的全部根,运行以下命令 m = [0,1,2] %为3个立方根而设
m =
0 1 2
R = abs(a)^(1/3)
%模的开3次方
R =
2
theta = (angle(a)+2pim)/3; %-pi<theta<=pi 的3个相位角 r = Rexp(itheta)
r =
1.0000 + 1.7321i -2.0000 + 0.0000i 1.0000 - 1.7321i 1.4.3 命令行的特殊输入法 1.输入多行命令并且不运行 输入命令后按shift +enter,然后光标会移动到下一行;在该行前不会显示命令提示符; 2.在同一行内输入多个函数; 在多个函数之间加入逗号或者分号将函数隔开;
x=(1:10)';logs=[x log10(x)]
logs =
1.0000 0
2.0000 0.3010
3.0000 0.4771
4.0000 0.6021
5.0000 0.6990
6.0000 0.7782
7.0000 0.8451
8.0000 0.9031
9.0000 0.9542
10.0000 1.0000
3、长命令行的分行输入 可以连用3个句号(...)作为标识符;然后按enter键输入其余命令,(...)用来表示下一行命令和上一行是连续的;然后可以继续用此方法输入。 例如可以使用以下命令对一个字符串数组进行赋值;
headers = ['author first name,author middle inital'...
'author last name']
headers =
author first name,author middle initalauthor last name
(...)如果出现在两个单引号中间,则会报错; 1.4.4 Command、 Window的显示格式 用户可以在命令行中输入format 函数对显示格式进行设置; 1.4.5 COMMAND WINDOW中查询帮助 1.help命令 1)获取指令说明 help exp exp - Exponential
This MATLAB function returns the exponential for each element of array X.
Y = exp(X)
exp 的参考页
另请参阅 expint, expm, expm1, log, log10, mpower, power
名为 exp 的其他函数
finance/exp
2)应用help分类搜索
help help
help - Help for functions in Command Window
This MATLAB function lists all primary help topics in the Command Window.
help
help name
help 的参考页
另请参阅 class, dbtype, doc, lookfor, more, path, what, which, whos
名为 help 的其他函数
simulink/help, dsp/help, distcomp/help, simevents/help, stateflow/help
3)应用help获得具体子类 输入 help elmat即可;
help elmat
Elementary matrices and matrix manipulation.
Elementary matrices.
zeros - Zeros array.
ones - Ones array.
eye - Identity matrix.
repmat - Replicate and tile array.
linspace - Linearly spaced vector.
logspace - Logarithmically spaced vector.
freqspace - Frequency spacing for frequency response.
meshgrid - X and Y arrays for 3-D plots.
accumarray - Construct an array with accumulation.
: - Regularly spaced vector and index into matrix.
Basic array information.
size - Size of array.
length - Length of vector.
ndims - Number of dimensions.
numel - Number of elements.
disp - Display matrix or text.
isempty - True for empty array.
isequal - True if arrays are numerically equal.
isequaln - True if arrays are numerically equal, treating NaNs as equal.
Matrix manipulation.
cat - Concatenate arrays.
reshape - Reshape array.
diag - Diagonal matrices and diagonals of matrix.
blkdiag - Block diagonal concatenation.
tril - Extract lower triangular part.
triu - Extract upper triangular part.
fliplr - Flip matrix in left/right direction.
flipud - Flip matrix in up/down direction.
flip - Flip the order of elements.
rot90 - Rotate matrix 90 degrees.
: - Regularly spaced vector and index into matrix.
find - Find indices of nonzero elements.
end - Last index.
sub2ind - Linear index from multiple subscripts.
ind2sub - Multiple subscripts from linear index.
bsxfun - Binary singleton expansion function.
Multi-dimensional array functions.
ndgrid - Generate arrays for N-D functions and interpolation.
permute - Permute array dimensions.
ipermute - Inverse permute array dimensions.
shiftdim - Shift dimensions.
circshift - Shift array circularly.
squeeze - Remove singleton dimensions.
Array utility functions.
isscalar - True for scalar.
isvector - True for vector.
isrow - True for row vector.
iscolumn - True for column vector.
ismatrix - True for matrix.
Special variables and constants.
eps - Floating point relative accuracy.
realmax - Largest positive floating point number.
realmin - Smallest positive floating point number.
intmax - Largest positive integer value.
intmin - Smallest integer value.
flintmax - Largest consecutive integer in floating point format.
pi - 3.1415926535897....
i - Imaginary unit.
inf - Infinity.
nan - Not-a-Number.
isnan - True for Not-a-Number.
isinf - True for infinite elements.
isfinite - True for finite elements.
j - Imaginary unit.
Specialized matrices.
compan - Companion matrix.
gallery - Test matrices.
hadamard - Hadamard matrix.
hankel - Hankel matrix.
hilb - Hilbert matrix.
invhilb - Inverse Hilbert matrix.
magic - Magic square.
pascal - Pascal matrix.
peaks - A sample function of two variables.
rosser - Classic symmetric eigenvalue test problem.
toeplitz - Toeplitz matrix.
vander - Vandermonde matrix.
wilkinson - Wilkinson's eigenvalue test matrix.
2.lookfor 命令 与help相似 查找不确切名称的函数时;例如lookfor 查询相关的函数
>> lookfor inverse
ifft - Inverse discrete Fourier transform.
ifft2 - Two-dimensional inverse discrete Fourier transform.
ifftn - N-dimensional inverse discrete Fourier transform.
ifftshift - Inverse FFT shift.
acos - Inverse cosine, result in radians.
acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
acot - Inverse cotangent, result in radian.
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
acsc - Inverse cosecant, result in radian.
acscd - Inverse cosecant, result in degrees.
acsch - Inverse hyperbolic cosecant.
asec - Inverse secant, result in radians.
asecd - Inverse secant, result in degrees.
asech - Inverse hyperbolic secant.
asin - Inverse sine, result in radians.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
atan - Inverse tangent, result in radians.
atan2 - Four quadrant inverse tangent.
atan2d - Four quadrant inverse tangent, result in degrees.
atand - Inverse tangent, result in degrees.
atanh - Inverse hyperbolic tangent.
invhilb - Inverse Hilbert matrix.
ipermute - Inverse permute array dimensions.
inv - Matrix inverse.
pinv - Pseudoinverse.
betaincinv - Inverse incomplete beta function.
erfcinv - Inverse complementary error function.
erfinv - Inverse error function.
gammaincinv - Inverse incomplete gamma function.
quatinv - Calculate the inverse of a quaternion.
revgeneticcode - creates a structure of inverse mappings for the genetic code.
qfuncinv - Inverse Q function.
dspblkistfft - Mask dynamic dialog function for inverse STFFT block
dspblkpinv - DSP System Toolbox Pseudo Inverse block helper function.
二、矩阵和数组 2.1 矩阵的创建于合并 2.1.1 简单矩阵
a = 100;
>> whos
Name Size Bytes Class Attributes
R 1x1 8 double
a 1x1 8 double
an 1x1 8 double
ans 1x1 8 double
b 2x2 64 double complex
c 2x2 64 double complex
headers 1x54 108 char
im 1x1 8 double
logs 10x2 160 double
m 1x3 24 double
r 1x3 48 double complex
sd 1x1 16 double complex
theta 1x3 24 double
x 10x1 80 double
例2-2:创建2-20区间内以2为步长的向量
a= 2:2:20
a =
2 4 6 8 10 12 14 16 18 20
以下命令中步长默认为1:
b= 1:10
b =
1 2 3 4 5 6 7 8 9 10
例2-3:矩阵中带符号的数值输入:
7-2+5
ans =
10
2.1.2创建特殊矩阵 常用的特殊矩阵的创建函数 zeros 创建所有元素为0的矩阵 diag 创建对角矩阵 ones 创建所有元素为1的矩阵 magic 创建魔方矩阵 eye 创建单位矩阵 pascal 创建PASCAL矩阵 Rand 随机产生均匀分布的矩阵 randn 随机产生正态分布的矩阵 randperm 产生一个由指定整数元素随机分布构成的矩阵 例如:
ones(4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> eye(5)
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> rand(2,3)
ans =
0.8147 0.1270 0.6324
0.9058 0.9134 0.0975
>> randperm(7)
ans =
5 1 2 7 3 4 6
>> rand('state',0) %设置随机种子状态
>> randperm(7)
ans =
2 7 4 3 6 5 1
2.1.3 矩阵的合并 将两两个或者多个矩阵合并到一起构成一个新的矩阵C=[A B]水平合并到一起;C = [A;B]在竖直方向合并到一起
rand('state',0);
>> A = ones(2,5)*6;
>> B = rand(3,5)
B =
0.9501 0.4860 0.4565 0.4447 0.9218
0.2311 0.8913 0.0185 0.6154 0.7382
0.6068 0.7621 0.8214 0.7919 0.1763
>> C = [A;B]
C =
6.0000 6.0000 6.0000 6.0000 6.0000
6.0000 6.0000 6.0000 6.0000 6.0000
0.9501 0.4860 0.4565 0.4447 0.9218
0.2311 0.8913 0.0185 0.6154 0.7382
0.6068 0.7621 0.8214 0.7919 0.1763
2.2 矩阵的寻访与赋值 全下标识符:行下标和列下标 2.2.2 矩阵的寻访
a = [1 2 3;4 5 6] %创建测试矩阵
a =
1 2 3
4 5 6
>> A = a(2,2) %采用全下标标识法
A =
5
>> b=a(5) %采用单下标标识法
b =
3
>> B=a>5 %返回逻辑下标;
B =
0 0 0
0 0 1
>>
>> c=a(B) %采用逻辑1标识法寻址
c =
6
>>
>> d=a(1,:) %采用冒号可以寻访全行元素
d =
1 2 3
>> e=a(:,2) %采用冒号可以寻访全列元素
e =
2
5
>> f=a(:) %采用单下标标识法寻访
f =
1
4
2
5
3
6
>> g=a(:,[1,3]) %寻访地址可以是向量,以同时寻访多个元素;
g =
1 3
4 6
2.2.3 矩阵的赋值 二维矩阵的赋值
a = magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
a(3,4)=0 %对单个元素进行赋值
a =
16 2 3 13
5 11 10 8
9 7 6 0
4 14 15 1
a(:,1)=1 %对第一列进行赋值
a =
1 2 3 13
1 11 10 8
1 7 6 0
1 14 15 1
a(14)=16 %采用单下标标识法对第14个元素进行赋值;
a =
1 2 3 13
1 11 10 16
1 7 6 0
1 14 15 1
2.3 进行数组运算的常用函数 常用的基本数学函数
|abs(x)| 绝对值或者向量的长度 |
|-angle(x)-|-复数z的相角-|
| sqrt(x) | 开平方 |
| real(z) | 复数z的实部 |
|-imag(z)-|-复数z的虚部-|
| conj(z) | 复数z的共轭复数 |
| round(x)|四舍五入至最近的整数|
|-fix(x)-|-无论正负,向0的方向取整数-|
| floor(x) | 用舍去法取最近的整数 |
|ceil(x)| 用进一法取最近的整数 |
|-rat(x)-|-将实数x转化为分数表示-|
| sign(x) | 符号函数0当x<0时,sign(x)=-1;当x=0时,sign(x)=0;当x>0时,sign(x)=1 |
| rem(x,y) | 求x除以y的余数|
|-gcd(x,y)-|-整数x和y的最大公因数-|
| lcm(x,y) | 整数x和y的最小公倍数 |
| exp(x)|自然指数|
|-pow2(x)-|-2的指数-|
| Log(x) | 以e为底的对数,即自然对数 |
|log2(x)| 以2为底的对数 |
|-log10(x)-|-以101为底的对数-|
数组运算
a=[1 2 4 8;16 25 36 49]
a = 1 2 4 8 16 25 36 49
b=sqrt(a) b =
1.0000 1.4142 2.0000 2.8284
4.0000 5.0000 6.0000 7.0000
2.4 查询矩阵信息 2.4.1 矩阵的形状信息 查询矩阵形状信息的函数 |length| 返回矩阵最长的一维的长度 | |-Ndims-|-返回矩阵的维数-| | numel | 返回矩阵的个数 | |size| 返回矩阵各维的长度 | 例:查询矩阵形状信息
rand('state',0); %设置随机种子,便于读者验证
A=rand(5)*10 %生成5*5的随机矩阵;
A =
9.5013 7.6210 6.1543 4.0571 0.5789
2.3114 4.5647 7.9194 9.3547 3.5287
6.0684 0.1850 9.2181 9.1690 8.1317
4.8598 8.2141 7.3821 4.1027 0.0986
8.9130 4.4470 1.7627 8.9365 1.3889
A(4:5,:)=[] %删除第4行和第5行
A =
9.5013 7.6210 6.1543 4.0571 0.5789
2.3114 4.5647 7.9194 9.3547 3.5287
6.0684 0.1850 9.2181 9.1690 8.1317
size(A)
ans =
3 5
a=length(A)
a =
5
>> b=sum(A(:))/numel(A) %使用sum和numel函数计算矩阵A的平均值
b =
5.8909
c=mean(mean(A)) %使用mean函数验证矩阵A的平均值
c =
5.8909
2.4.2 矩阵的数据类型 判断矩阵数据类型的函数
|isa| 判断输入矩阵是否是给定类型 | |-iscell-|-判断输入矩阵是否为cell数组-| | iscellstr | 判断输入矩阵是否是由字符串构成的cell数组 | | ischar | 判断输矩阵是否为字符串数组 | |-isfloat-|-判断输入矩阵是否为浮点数组-| | isinteger | 判断输入矩阵是否为整数数组 | |islogical| 判断输入矩阵是否为逻辑数组 | |-isnumeric-|-判断输入矩阵是否为数值数组-| | isreal | 判断输入的矩阵是否为实数数组 | |-isstruct-|-判断输入矩阵是否为structure数组-|
2.4.3矩阵的数据结构 | isempty | 判断输入矩阵是否为空 | |isscaler| 判断输入矩阵是否为1x1的标量 | |-issparse-|-判断输入的矩阵是否为稀疏矩阵-| | isvector | 判断输入的矩阵是否为向量 |