Friday, May 1, 2009

MATLAB: Multiplying with a Matrix Inverse

M-Lint has detected a call to inv in a multiplication operation.
The inverse of a matrix is primarily of theoretical value, and rarely finds any use in practical computations. Never use it to solve a linear system Ax=b with x=inv(A)*b, because it is slow and inaccurate.

Suggested Action
Instead of multiplying the matrix by its inverse, use matrix right division (/) or matrix left division (\). That is:
Replace inv(A)*b with A\b
Replace b*inv(A) with b/A
Frequently, an application needs to solve a series of related linear systems Ax=b, where A does not change, but b does. In this case, use lu, chol, or qr instead of inv, depending on the matrix type.

No comments: