1、数字
2、字符串
s='This is a string'
>> a='a'
a =
'a'
>> abs(a) %求字符a的ASCII码
ans =
97
>> char(97) %求ASCII码=97的字符
ans =
'a'
>> num2str(65) %把65这个数字转换成字符串'65'
ans =
'65'
>> str2num('65') %把字符串'65'转换成65这个数字
ans =
65
3、矩阵
1)建立矩阵变量方法
i.
>> A=[1 2 3
4 5 6
7 8 9]
A =
1 2 3
4 5 6
7 8 9
ii.
>> B=[0 0 0;0 0 0;0 0 0]
B =
0 0 0
0 0 0
0 0 0
两种方法都可以
2)转置矩阵
B=A'
>> B=A'
B =
1 4 7
2 5 8
3 6 9
3)矩阵转化成列向量
C=A(:)
沿着列转化
>> C=A(:)
C =
1
4
7
2
5
8
3
6
9
对比A矩阵:
A =
1 2 3
4 5 6
7 8 9
4)求逆矩阵
D=inv(A)
>> D=inv(A)
警告: 矩阵接近奇异值,或者缩放错误。结果可能不准确。RCOND = 2.202823e-18。
D =
1.0e+16 *
0.3153 -0.6305 0.3153
-0.6305 1.2610 -0.6305
0.3153 -0.6305 0.3153
5)生成零矩阵
使用函数zeros(m,n)
>> Z=zeros(10,5) %生成了一个10行5列的零矩阵
Z =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
6)生成三维零矩阵
zeros(m,n,k)
>> Z1=zeros(2,3,3) %生成2行,3列,3层的零矩阵
Z1(:,:,1) =
0 0 0
0 0 0
Z1(:,:,2) =
0 0 0
0 0 0
Z1(:,:,3) =
0 0 0
0 0 0
7)生成随机数矩阵
>> R=rand(5,5)
R =
0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787
>> rand(5,5)
ans =
0.7577 0.7060 0.8235 0.4387 0.4898
0.7431 0.0318 0.6948 0.3816 0.4456
0.3922 0.2769 0.3171 0.7655 0.6463
0.6555 0.0462 0.9502 0.7952 0.7094
0.1712 0.0971 0.0344 0.1869 0.7547
>> randn(5,5)
ans =
-0.8637 1.5326 -1.0891 0.0859 -0.6156
0.0774 -0.7697 0.0326 -1.4916 0.7481
-1.2141 0.3714 0.5525 -0.7423 -0.1924
-1.1135 -0.2256 1.1006 -1.0616 0.8886
-0.0068 1.1174 1.5442 2.3505 -0.7648
>> randi(10)
ans =
2
>> randi(100,5,5)
ans =
26 59 76 54 47
62 55 39 78 2
48 92 57 94 34
36 29 8 13 17
84 76 6 57 80
>> randi([30,70],5,5)
ans =
42 56 39 70 30
51 58 67 33 61
36 60 36 48 63
54 48 63 34 65
40 33 52 69 33