计算逆矩阵需要注意的问题
计算逆矩阵需要注意的问题:
方阵:
A = rand(5)
A =
0.5492 0.6463 0.8491 0.6685 0.6669
0.7284 0.5212 0.3725 0.2068 0.9337
0.5768 0.3723 0.5932 0.6539 0.8110
0.0259 0.9371 0.8726 0.0721 0.4845
0.4465 0.8295 0.9335 0.4067 0.7567
B = rand(1,5)
B = B'
B =
0.5583
0.5989
0.1489
0.8997
0.4504
方阵计算方法1:
C = A\B
C =
-0.9242
5.7732
-5.1228
2.9991
-0.4805
方阵计算方法2:
C1 = inv(A)*B
C1 =
-0.9242
5.7732
-5.1228
2.9991
-0.4805
方阵计算方法3:
C2 = (inv(A'*A)*A')*B
C2 =
-0.9242
5.7732
-5.1228
2.9991
-0.4805
方阵计算方法4:
C3 = (A'*inv(A*A'))*B
C3 =
-0.9242
5.7732
-5.1228
2.9991
-0.4805
方阵计算方法5:
[P,Q]=mypq(A);
C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B
C4 =
-0.9242
5.7732
-5.1228
2.9991
-0.4805
简单非方阵:
A = rand(5,4)
A =
0.6575 0.1343 0.3017 0.6311
0.9509 0.0605 0.0117 0.8593
0.7223 0.0842 0.5399 0.9742
0.4001 0.1639 0.0954 0.5708
0.8319 0.3242 0.1465 0.9969
简单非方阵计算方法1:
C = A\B
C =
0.3940
1.0239
-0.5243
0.1851
简单非方阵计算方法2:
C1 = inv(A)*B
Error using inv
Matrix must be square.
简单非方阵计算方法3:
C2 = (inv(A'*A)*A')*B
C2 =
0.3940
1.0239
-0.5243
0.1851
简单非方阵计算方法4:
C3 = (A'*inv(A*A'))*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.039534e-18.
C3 =
-4.0888
-14.5982
-6.9755
-36.5726
简单非方阵计算方法5:
C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.039534e-18.
Error using *
Inner matrix dimensions must agree.
复杂非方阵:
x = linspace(-3,3,n)';
A(:,1) = ones(n,1);
for j=1:15
A(:,2*j) = sin(j/2*x);p(:,2*j+1) = cos(j/2*x);
end
B = rand(50,1);
复杂非方阵计算方法1:
C = A\B
C =
1.0e+10 *
-3.9512
0.0613
7.5360
-0.1063
-6.5327
0.1255
5.1400
复杂非方阵计算方法2:
C1 = inv(A)*B
Error using inv
Matrix must be square.
复杂非方阵计算方法3:
C2 = (inv(A'*A)*A')*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.971633e-18.
C2 =
1.0e+13 *
-0.0000
-6.5291
-5.2530
1.8172
1.6742
-3.1518
2.0103
复杂非方阵计算方法4:
C3 = (A'*inv(A*A'))*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 6.046658e-19.
C3 =
1.0e+14 *
1.2922
-0.4676
0.7775
-0.1933
-0.1726
0.6507
-0.4371
复杂非方阵计算方法5:
C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 6.046658e-19.
Error using *
Inner matrix dimensions must agree.
问题:
对于简单非方阵只有方法1和3可用,对于复杂非方阵只有方法1可用!
将以上方法中的inv替换成pinv就正确了,matlab中的pinv函数的实现是用svd实现的:
[U,S,V] = svd(A,'econ');
s = diag(S);
if nargin < 2
tol = max(size(A)) * eps(norm(s,inf));
end
r1 = sum(s > tol)+1;
V(:,r1:end) = [];
U(:,r1:end) = [];
s(r1:end) = [];
s = 1./s(:);
X = bsxfun(@times,V,s.')*U';
现在还不明白为什么svd可以,而以上方法不可以?
- 仿脑计算需要注意的几个问题
- 使用matlab计算DFT时需要注意的幅值对应问题
- Java日期计算天数差需要注意的问题
- BigDecimal类的使用(精确计算时需要注意问题)
- 单片机计算需要注意的问题
- win7 64位下自行编译OpenCV2.4.10+CUDA toolkit 5.5的整个过程以及需要注意的问题(opencv+cuda并行计算元素的使用)
- win7 64位下自行编译OpenCV2.4.10+CUDA toolkit 5.5的整个过程以及需要注意的问题(opencv+cuda并行计算元素的使用)
- win7 64位下自行编译OpenCV2.4.10+CUDA toolkit 5.5的整个过程以及需要注意的问题(opencv+cuda并行计算元素的使用)
- 计算DOM元素的top时需要注意的一个问题
- double float 类型的数据(小数)在作计算的时候,需要注意的。(Java和JavaScript)都有这问题
- OC语言中延展,类目需要注意的几个小问题
- 返回引用需要注意的问题
- 教育行业SaaS选型 需要注意的三点问题
- sprintf使用时需要注意的问题
- 10个 DIV+CSS 需要注意的问题
- 数据恢复过程中需要注意的一些问题
- list嵌套需要注意的问题
- 关于相反数输出时需要注意的问题
- 机器学习算法需要注意的一些问题,算法一些问题
- Android 选择文件、拍照等需要注意的问题