The Moore–Penrose pseudo inverse, which is the basis for Matab and octave's pinv
, is implemented via completely different algorithm than the inv
function. More specifically, singular value decomposition is used, which require's finite-valued matrices (they also can't be sparse
). You didn't say if your matrices are square or not. The real use of pinv
is for solving non-square systems (over- or underdetermined).
However, you shouldn't be using pinv
or inv
for your application, no matter the dimension of your matrices. Instead you should use mldivide
(octave, Matlab), i.e., the backslash operator, \
. This is much more efficient and numerically robust.
A1 = 3;
A2 = [1 2 1;2 4 6;1 1 3];
A1inv = A1\1
A2inv = A2\eye(size(A2))
The mldivide
function handles rectangular matrices too, but you will get different answers for underdetermined systems compared to pinv
because the two use different methods to choose the solution.
A3 = [1 2 1;2 4 6]; % Underdetermined
A4 = [1 2;2 4;1 1]; % Overdetermined
A3inv = A3\eye(min(size(A3))) % Compare to pinv(A3), different answer
A4inv = A4\eye(max(size(A4))) % Compare to pinv(A4), same answer
If you run the code above, you'll see that you get a slightly different result for A3inv
as compared to what is returned by pinv(A3)
. However, both are valid solutions.