34.逆矩阵
>> A=[1 2 3 4;2 3 5 8;1 3 5 7;3 4 7 11]
A =
1 2 3 4
2 3 5 8
1 3 5 7
3 4 7 11
>> det(A) %判断A的行列式是否为0
ans =
1.0000
>> inv(A) %矩阵的逆矩阵函数
ans =
1.0000 -1.0000 -1.0000 1.0000
2.0000 4.0000 -1.0000 -3.0000
0 -5.0000 1.0000 3.0000
-1.0000 2.0000 -0.0000 -1.0000
>> inv(A)*A %验证A的逆和A的乘积是否为单位矩阵
ans =
1.0000 0 0.0000 0
0.0000 1.0000 0 0
0 0.0000 1.0000 0.0000
0 0 -0.0000 1.0000
>> A\eye(4) %单位矩阵左除A求逆矩阵
ans =
1.0000 -1.0000 -1.0000 1.0000
2.0000 4.0000 -1.0000 -3.0000
0 -5.0000 1.0000 3.0000
-1.0000 2.0000 -0.0000 -1.0000
>> eye(4)/A %单位矩阵右除A求逆矩阵
ans =
1.0000 -1.0000 -1.0000 1.0000
2.0000 4.0000 -1.0000 -3.0000
0 -5.0000 1.0000 3.0000
-1.0000 2.0000 -0.0000 -1.0000
>> A^(-1) %用A的(-1)次乘幂求逆矩阵
ans =
1.0000 -1.0000 -1.0000 1.0000
2.0000 4.0000 -1.0000 -3.0000
0 -5.0000 1.0000 3.0000
-1.0000 2.0000 -0.0000 -1.0000
>> A=[1 2 3 4;2 3 5 8;1 3 5 7;3 4 7 11]
A =
1 2 3 4
2 3 5 8
1 3 5 7
3 4 7 11
>> B=[A,eye(size(A))] %构造增广矩阵B,由A和同阶单位矩阵水平连接而成
B =
1 2 3 4 1 0 0 0
2 3 5 8 0 1 0 0
1 3 5 7 0 0 1 0
3 4 7 11 0 0 0 1
>> rref(B) %rref函数把B化成行最简形
ans =
1 0 0 0 1 -1 -1 1
0 1 0 0 2 4 -1 -3
0 0 1 0 0 -5 1 3
0 0 0 1 -1 2 0 -1
>> C=ans(:,5:8) %矩阵C的5-8列即为A的逆矩阵
C =
1 -1 -1 1
2 4 -1 -3
0 -5 1 3
-1 2 0 -1
>> inv(A) %用求逆公式验证逆矩阵
ans =
1.0000 -1.0000 -1.0000 1.0000
2.0000 4.0000 -1.0000 -3.0000
0 -5.0000 1.0000 3.0000
-1.0000 2.0000 -0.0000 -1.0000
35.矩阵的LU分解
>> A=[1 3 5 7;2 4 6 8;2 3 5 8;3 4 7 11]
A =
1 3 5 7
2 4 6 8
2 3 5 8
3 4 7 11
>> [l,u]=lu(A)
l =
0.3333 1.0000 0 0
0.6667 0.8000 1.0000 0
0.6667 0.2000 0.2500 1.0000
1.0000 0 0 0
u =
3.0000 4.0000 7.0000 11.0000
0 1.6667 2.6667 3.3333
0 0 -0.8000 -2.0000
0 0 0 0.5000
>> l*u
ans =
1 3 5 7
2 4 6 8
2 3 5 8
3 4 7 11
36.矩阵的正交分解
>> A=magic(3)
A =
8 1 6
3 5 7
4 9 2
>> [q,r]=qr(A)
q =
-0.8480 0.5223 0.0901
-0.3180 -0.3655 -0.8748
-0.4240 -0.7705 0.4760
r =
-9.4340 -6.2540 -8.1620
0 -8.2394 -0.9655
0 0 -4.6314
>> q*r
ans =
8.0000 1.0000 6.0000
3.0000 5.0000 7.0000
4.0000 9.0000 2.0000
>> q*q'
ans =
1.0000 -0.0000 -0.0000
-0.0000 1.0000 0
-0.0000 0 1.0000
37.矩阵的Cholesky分解
>> A=[1 1 1 1;1 3 3 3;1 3 5 5;1 3 5 7]
A =
1 1 1 1
1 3 3 3
1 3 5 5
1 3 5 7
>> eig(A)
ans =
0.5198
0.7232
1.6199
13.1371
>> chol(A)
ans =
1.0000 1.0000 1.0000 1.0000
0 1.4142 1.4142 1.4142
0 0 1.4142 1.4142
0 0 0 1.4142
>> T=ans;
>> T'*T
ans =
1.0000 1.0000 1.0000 1.0000
1.0000 3.0000 3.0000 3.0000
1.0000 3.0000 5.0000 5.0000
1.0000 3.0000 5.0000 7.0000
38.广义逆矩阵
>> A=[1 2 3;4 5 6;1 3 5;2 5 8;3 7 9]
A =
1 2 3
4 5 6
1 3 5
2 5 8
3 7 9
>> rank(A)
ans =
3
>> pinv(A)
ans =
0.0870 0.5435 0.0217 0.1087 -0.5000
-0.2050 -0.3168 -0.3727 -0.5776 1.0000
0.1304 0.0652 0.2826 0.4130 -0.5000
39.数组和矩阵的乘幂
>> x=magic(3)
x =
8 1 6
3 5 7
4 9 2
>> y=[1 2 3;4 5 6;7 8 9]
y =
1 2 3
4 5 6
7 8 9
>> V=power(x,y)
V =
8 1 216
81 3125 117649
16384 43046721 512
>> V=x.^y
V =
8 1 216
81 3125 117649
16384 43046721 512
>> A=[1 2;3 4]
A =
1 2
3 4
>> sqrt(A)
ans =
1.0000 1.4142
1.7321 2.0000
>> G=mpower(A,1/2)
G =
0.5537 + 0.4644i 0.8070 - 0.2124i
1.2104 - 0.3186i 1.7641 + 0.1458i
>> G=A^(1/2)
G =
0.5537 + 0.4644i 0.8070 - 0.2124i
1.2104 - 0.3186i 1.7641 + 0.1458i
>> G*G
ans =
1.0000 + 0.0000i 2.0000 + 0.0000i
3.0000 + 0.0000i 4.0000 + 0.0000i
40.矩阵的水平连接和垂直连接
>> A=magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> B=ones(4,3)
B =
1 1 1
1 1 1
1 1 1
1 1 1
>> C=horzcat(A,B)
C =
16 2 3 13 1 1 1
5 11 10 8 1 1 1
9 7 6 12 1 1 1
4 14 15 1 1 1 1
>> A=rand(2,5)
A =
0.8147 0.1270 0.6324 0.2785 0.9575
0.9058 0.9134 0.0975 0.5469 0.9649
>> B=[1 2 3 4 5;3 5 7 9 11]
B =
1 2 3 4 5
3 5 7 9 11
>> C=vertcat(A,B)
C =
0.8147 0.1270 0.6324 0.2785 0.9575
0.9058 0.9134 0.0975 0.5469 0.9649
1.0000 2.0000 3.0000 4.0000 5.0000
3.0000 5.0000 7.0000 9.0000 11.0000
41.矩阵的复制
>> V=[2 3 5 8];
>> m=10;n=1;
>> B=repmat(V,m,n)
B =
2 3 5 8
2 3 5 8
2 3 5 8
2 3 5 8
2 3 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 0 21 0 55
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
>> nnz(S) %nnz为非零元素个数的计数函数
ans =
12
>> density=ans/(m*n) %稀疏矩阵的密度:非零元素数与矩阵总元素之比
density =
0.2500
>> A=2*rand(4,5)
A =
1.6294 1.2647 1.9150 1.9143 0.8435
1.8116 0.1951 1.9298 0.9708 1.8315
0.2540 0.5570 0.3152 1.6006 1.5844
1.8268 1.0938 1.9412 0.2838 1.9190
>> A=A>1
A =
4×5 logical 数组
1 1 1 1 0
1 0 1 0 1
0 0 0 1 1
1 1 1 0 1
>> B=spdiags(A,[-2,-1,0,1,2],4,6)
B =
4×6 稀疏 logical 数组
(1,1) 1
(1,2) 1
(2,2) 1
(4,2) 1
(4,3) 1
(2,4) 1
(3,4) 1
(4,4) 1
(3,5) 1
(4,6) 1
>> C=full(B)
C =
4×6 logical 数组
1 1 0 0 0 0
0 1 0 1 0 0
0 0 0 1 1 0
0 1 1 1 0 1
43.稀疏矩阵的图形显示
>> A=magic(8)>30
A =
8×8 logical 数组
1 0 0 1 1 0 0 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 0 0 1 1 0 1 1
1 1 1 0 0 1 1 0
1 0 0 1 1 0 0 1
1 0 0 1 1 0 0 1
0 1 1 0 0 1 1 0
>> B=spdiags(A,[-2,-1,0,1,2],8,12)
B =
8×12 稀疏 logical 数组
(2,1) 1
(1,2) 1
(2,2) 1
(3,2) 1
(4,2) 1
(1,3) 1
(3,3) 1
(5,3) 1
(5,4) 1
(6,4) 1
(4,5) 1
(5,5) 1
(7,5) 1
(4,6) 1
(6,7) 1
(8,7) 1
(6,8) 1
(7,8) 1
(8,8) 1
(7,9) 1
>> spy(B)
44.寻找矩阵的非零元素
>> A=[2 3 5 8 13 21 34];
>> E=ones(6,1);
>> B=diag(A)+diag(E,1)+diag(E,-1) % diag(v,k):以向量v的元素作为矩阵X的第k条对角线元素,当k=0时,v为X的主对角线;当k>0时,v为上方第k条对角线;当k<0时,v为下方第k条对角线。
B =
2 1 0 0 0 0 0
1 3 1 0 0 0 0
0 1 5 1 0 0 0
0 0 1 8 1 0 0
0 0 0 1 13 1 0
0 0 0 0 1 21 1
0 0 0 0 0 1 34
>> k=find(B)
k =
1
2
8
9
10
16
17
18
24
25
26
32
33
34
40
41
42
48
49
>> [i,j]=find(B);
>> i'
ans =
1 2 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7
>> j'
ans =
1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7
>> [i,j,v]=find(B);
>> v'
ans =
2 1 1 3 1 1 5 1 1 8 1 1 13 1 1 21 1 1 34
>> nnz(B)
ans =
19