inverse of a matrix in matlab

You can use the inv function to find the inverse of a matrix in MATLAB. Given a matrix A, the syntax is:

main.m
A_inv = inv(A)
15 chars
2 lines

Here's an example:

main.m
A = [1 2; 3 4];
A_inv = inv(A);
32 chars
3 lines

The output would be:

main.m
A_inv =
   -2.0000    1.0000
    1.5000   -0.5000
50 chars
4 lines

Note that not all matrices have an inverse, and in general it's best to avoid using inv when possible, as it can be numerically unstable. Instead, you can use \ to solve linear equations, like so:

main.m
x = A \ b
10 chars
2 lines

where b is a column vector representing the right-hand side of the equation Ax = b.

related categories

gistlibby LogSnag