Singularity in matrix when inverting in Matlab

12.2k Views Asked by At

As data I get a matrix A but in my algorithm I need to work on its inverse. What I do is:

C = inv(A) + B;

Then in another line I update A. In the next cycles I also need (updated) A inverse, again for this algorithm. And so on. In the later cycles I get this:

Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.425117e-019

or this:

Warning: Matrix is singular to working precision.

or this:

Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.

Can you help me how to avoid such singularity? Matrix is squared always.

2

There are 2 best solutions below

0
On

It definitely depends on what you are doing and what matrix A represents (in some cases there might be programming error causing A become singular). But if the singularity of A is unavoidable, you can use the Moore-Penrose pseudoinverse as an alternative to inverse matrix which has most of the properties of an inverse matrix (See it in wiki).

The equivalent command in Matlab would be pinv.

0
On

You'd get yelled at and downvoted if you tried to post this over at StackOverflow. ;-)

The problem might be with you use of the function inv, which has a spotty reputation in Matlab circles. Try this:

C = A\eye(size(A)) + B;

Do you still get warnings? The trick is that here I used the backslash operator \, mldivide, left matrix divide. This solves for the inverse of A by much more numerically stable, and often much faster methods. If you still had warnings you should check the rank of A and read up on solving systems of linear equations and pinv as @pm89 suggested.