简体   繁体   中英

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. My all accuracy depends upon inv(D'*D) which is not correct. Is there any alternate way to get inv(D'*D) accurately? can any one help me please?

% D is 3x4 matrix that i had copied from one blog just for demonstration purpose. 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. 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. So i want to find final D inverse using the inverse which i calculated before these two steps. 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.

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.

Try running cond(D) on your matrix and see what the condition number is. The higher the number, the more ill-conditioned your matrix is. Similarly, you can run 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 . 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. For example, if you just need the pseudo-inverse, go ahead and use pinv() , even though it differs from your result using inv() . The pinv() function and the \\ (mldivide) backslash operator are much more numerically stable tools than inv() .

See the official documentation at 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. See the numerical example at the link above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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