The most efficient matrix inversion in MATLAB

When computing the inverse of some square matrix A in MATLAB, using

Ai = inv(A) % should be the same as: Ai = A^-1 

MATLAB usually notifies me that this is not the most efficient way to handle it. So which is more efficient? If I have a system of equations, then probably there is /, \ operator. But sometimes I need the opposite for other calculations.

What is the most efficient way to invert?

+6
matrix matlab linear-algebra matrix-inverse
source share
4 answers

I would recommend using svd (unless you are really sure that your matrix is ​​not poorly prepared). Then, based on the special meanings, you make decisions about further actions. This may sound like a bust, but in the long run it will pay off.

Now, if your matrix A is actually invertible, then pseudo inverse of A same as inv(A) , however, if you are close to “singularity”, you can easily make the appropriate decision about how to actually make pseudo inverse . Naturally, these decisions will depend on your application.

A simple example is added :

 > A= randn(3, 2); A= [AA(:, 1)+ A(:, 2)] A = -1.520342 -0.239380 -1.759722 0.022604 0.381374 0.403978 0.852420 1.521925 2.374346 > inv(A) warning: inverse: matrix singular to machine precision, rcond = 0 ans = Inf Inf Inf Inf Inf Inf Inf Inf Inf > [U, S, V]= svd(A) U = -0.59828 -0.79038 0.13178 0.13271 -0.25993 -0.95646 0.79022 -0.55474 0.26040 S = Diagonal Matrix 3.6555e+000 0 0 0 1.0452e+000 0 0 0 1.4645e-016 V = 0.433921 0.691650 0.577350 0.382026 -0.721611 0.577350 0.815947 -0.029962 -0.577350 > s= diag(S); k= sum(s> 1e-9) % simple thresholding based decision k = 2 > Ainv= (U(:, 1: k)* diag(1./ s(1: k))* V(:, 1: k)')' Ainv = -0.594055 -0.156258 -0.273302 0.483170 0.193333 0.465592 -0.110885 0.037074 0.192290 > A* Ainv ans = 0.982633 0.126045 -0.034317 0.126045 0.085177 0.249068 -0.034317 0.249068 0.932189 > A* pinv(A) ans = 0.982633 0.126045 -0.034317 0.126045 0.085177 0.249068 -0.034317 0.249068 0.932189 
+10
source share

I think LU decomposition is more efficient than inversion (and potentially more stable if you use rotation). It works especially well if you need to solve more than one vector of the right side, because as soon as you have LU decomposition, you can make backward replacements for each of them when you need it.

I would recommend LU decomposition in the complete opposite. I agree with what MATLAB says.

UPDATE: 3x3 matrix? You can invert this manually in closed form if you need it. Just check the qualifier first to make sure it is not singular or almost singular.

+3
source share

If you only need the opposite, then just doing it will be numerically more stable than inv (A):

 inv_A = 1\A; 
+3
source share

If there is no sensible way to do all your calculations without explicitly forming the opposite, then you need to use the inv function. Of course, you could solve a linear system with your matrix and identity matrix, but nothing will be done.

0
source share

All Articles