1.特殊矩阵
>> x=rand(5)
x =
736/1841 573/3151 1531/2641 737/2100 486/3941
441/1697 2131/8078 590/1073 368/717 8201/44593
2337/2921 752/5167 385/2656 489/1217 203/846
412/955 524/3851 563/660 666/8767 3417/8189
1223/1343 552/635 660/1061 458/1909 273/5498
>> A=fix(10+(99-10+1)*x)
A =
45 26 62 41 21
33 33 59 56 26
82 23 23 46 31
48 22 86 16 47
91 88 65 31 14
>> B=0.6+sqrt(0.1)*randn(5)
B =
1675/1851 604/655 235/568 2174/1935 159/187
542/709 535/959 1213/2392 1877/2692 241/1348
1781/3000 49/131 376/1133 723/3571 -353/2581
1982/3365 2179/2121 481/1957 4231/12966 147/1037
398/1145 951/1798 463/331 696/1279 697/988
>> I=eye(5)
I =
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
>> (A+B)*I==I*A+B*I
ans =
5×5 logical 数组
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
>> M=magic(5)
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> sum(M(1,:))==sum(M(:,1))
ans =
logical
1
2.矩阵变换
>> x=[1:3;4:6;7:9]
x =
1 2 3
4 5 6
7 8 9
>> diag(x)
ans =
1
5
9
>> y=[diag(x,1);diag(x,-1)]
y =
2
6
4
8
>> %triu() tri-----三角;u-----up上
%tril() tri-----三角;l-----low上
>> triu(ones(4),-2)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
0 1 1 1
>> tril(ones(4),-2)
ans =
0 0 0 0
0 0 0 0
1 0 0 0
1 1 0 0
>> x=[1:4;5:8;9:12]
x =
1 2 3 4
5 6 7 8
9 10 11 12
>> rot90(x)
ans =
4 8 12
3 7 11
2 6 10
1 5 9
>> rot90(x,2)
ans =
12 11 10 9
8 7 6 5
4 3 2 1
>> rot90(x,0.5)
错误使用 rot90 (line 38)
k 必须为整数。
>> %利用flipud和diag可以获得矩阵的副对角线
>> x=fix(10*rand(5))
x =
1 6 3 8 5
2 7 4 0 2
4 2 5 9 4
5 1 0 7 9
2 2 2 4 5
>> %主对角线
>> diag(x)
ans =
1
7
5
7
5
>> %副对角线
>> diag(flipud(x))
ans =
2
1
5
0
5
>> %可以看到,这样做的副对角线是从左下角到右上角
3.矩阵求值