简体   繁体   English

伪逆的手动计算与Matlab中的pinv不同

[英]manual calculation of pseudo inverse is not same as pinv in matlab

I had a matrix D which is m*n and i am calculating the pseudo inverse using the formula inv(D'*D)*D' but it is not generating the same result as pinv(D).I need the term inv(D'*D) which i require for incremental operation. 我有一个矩阵D,它是m * n,我正在使用公式inv(D'* D)* D'计算伪逆,但它不会产生与pinv(D)相同的结果。我需要inv( D'* D),我需要进行增量操作。 My all accuracy depends upon inv(D'*D) which is not correct. 我所有的准确性取决于不正确的inv(D'* D)。 Is there any alternate way to get inv(D'*D) accurately? 是否有其他方法可以准确获得inv(D'* D)? can any one help me please? 有人可以帮我吗?

% D is 3x4 matrix that i had copied from one blog just for demonstration purpose. %D是我从一个博客复制而来的3x4矩阵,仅用于演示目的。 Actually original one of mine also had same problem bu its size is too large that i can't post it here. 实际上,我的原始版本也有相同的问题,因为它的尺寸太大,我无法在此处发布。

D = -[1/sqrt(2) 1 1/sqrt(2) 0;0 1/sqrt(2) 1 1/sqrt(2);-1/sqrt(2) 0 1/sqrt(2) 1];

    B1 = pinv(D)
    B2 = D'*inv(D*D')

    B1 =

      -0.353553390593274   0.000000000000000   0.353553390593274
      -0.375000000000000  -0.176776695296637   0.125000000000000
      -0.176776695296637  -0.250000000000000  -0.176776695296637
       0.125000000000000  -0.176776695296637  -0.375000000000000

    Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =
    1.904842e-017. 

    B2 =

      -0.250000000000000                   0   0.500000000000000
      -0.500000000000000                   0                   0
       0.250000000000000  -0.500000000000000                   0
                       0                   0  -0.750000000000000

I need inv(D'D) to do incremental operation. 我需要inv(D'D)进行增量操作。 Actually in my problem at step 1, each time a new row will be added to the last position of D and in step 2 first row of the D will be removed. 实际上,在我在步骤1中遇到的问题中,每次将新行添加到D的最后一个位置,而在步骤2中,D的第一行将被删除。 So i want to find final D inverse using the inverse which i calculated before these two steps. 所以我想使用我在这两个步骤之前计算的逆来找到最终的D逆。 More precisely have a look here: 更精确地在这里看看:

B = inv(D'*D); % if i can calculate it accurately then further work is as follows
D1 = [D;Lr];  %Lr is last row to be added
BLr = B-((B*Lr'*Lr*B)/(1+Lr*B*Lr')); % Row addition formula
Fr = D1(1,:); % First row to be removed 
D2 = removerows(D1,1);
BFr = BLr+ ((BLr*Fr'*Fr*BLr)/(1-Fr*BLr*Fr')); % row deletion formula
B = BFr;
Y = BFr*D2;

The formulae (D^TD)^-1 D^T or D^T (DD^T)^-1 you are using for the Moore-Penrose pseudoinverse are only valid if D has full column or full row rank, respectively. 您用于Moore-Penrose伪逆的公式(D ^ TD)^-1 D ^ T或D ^ T(DD ^ T)^-1仅在D具有完整的列或完整的行秩时才有效。

This is not true in your case, as the warning "Matrix is close to singular" shows. 在您的情况下,情况并非如此,因为警告“矩阵接近单数”显示。

The matlab pinv command works for arbitrary D, even if the matrix has neither full row or full column rank. 即使矩阵没有完整的行或完整的列秩,matlab pinv命令也适用于任意D。

Try running cond(D) on your matrix and see what the condition number is. 尝试在矩阵上运行cond(D)并查看条件编号多少 The higher the number, the more ill-conditioned your matrix is. 数字越高,矩阵的状态越差。 Similarly, you can run cond(D'*D) . 同样,您可以运行cond(D'*D) A matrix can be full rank and still be ill-conditioned. 矩阵可以是全等级的,但仍然是病态的。 On paper, an ill-conditioned matrix is still invertible. 在纸上,病态矩阵仍然是可逆的。 However, when you attempt to directly invert an ill-conditioned matrix on a computer, small precision errors caused by quantization and other effects can cause wildly undpredictable results in the solution. 但是,当您尝试将计算机上的病态矩阵直接求逆时,由量化和其他影响引起的小精度误差可能会在解决方案中导致难以预测的结果。

For the above stated reason, there is usually a better way (more numerically stable) to achieve what you are after than to compute the inverse directly. 由于上述原因,通常有比直接计算逆数更好的方法(数值上更稳定)来实现目标。 Many of these involve matrix decomposotion techniques such as SVD . 其中许多涉及矩阵分解技术,例如SVD If you help us understand why you need inv(D'*D) it would be easier to point you in the direction of the appropriate alternative. 如果您帮助我们理解了为什么需要inv(D'*D) ,将使您更容易找到合适的选择。 For example, if you just need the pseudo-inverse, go ahead and use pinv() , even though it differs from your result using inv() . 例如,如果只需要伪逆,则继续使用pinv() ,即使它与inv()结果有所不同。 The pinv() function and the \\ (mldivide) backslash operator are much more numerically stable tools than inv() . pinv()函数和\\ (mldivide) 反斜杠运营商都远远超过数值稳定工具inv()

See the official documentation at http://www.mathworks.com/help/matlab/ref/pinv.html . 请参阅http://www.mathworks.com/help/matlab/ref/pinv.html上的官方文档。

If A x ~ b, the solution x = pinv(A) * b produces the minimum-norm solution, but x = A\\b doesn't. 如果A x = pinv(A) * b ,则解x = pinv(A) * b产生最小范数解,但x = A\\b不会。 See the numerical example at the link above. 参见上面链接的数值示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM