I have noticed that if A is a NxN matrix and it has the inverse matrix. But what the inv() and pinv() function output is different. - My environment is Win7x64 SP1, Matlab
It seems to me like you answered your own question in the bottom here. The reason is floating point arithmetic. The algortihms for inv()
and pinv()
are not exactly the same, as pinv()
must be able to handle non-square matrices. Therefore the answers will not be exactly the same.
If you look at the value of pinv(A)*A
, you will see that it's very very close to the identity matrix.
I get:
ans =
1.0000e+00 6.1062e-16 -3.0809e-15
-5.8877e-15 1.0000e+00 6.3942e-15
2.4425e-15 -3.0184e-16 1.0000e+00
Instead of comparing the matrices with ==
, use < tolerance_limit
c = A*pinv(A);
d = pinv(A)*A;
(c-d) < 1e-10
Sidenote:
x = A^-1*b
should not be solved x = inv(A)*b;
, but rather x = A \ b;
See the link Shai posted for explanations.