get inverse matrix in matlab

One way to get the inverse of a matrix in MATLAB is to use the inv function. The syntax is as follows:

main.m
inv_A = inv(A);
16 chars
2 lines

where A is the matrix you want to invert and inv_A is the inverse of A. Note that not all matrices are invertible, so you may need to check for singularity before attempting to calculate the inverse.

Here is an example:

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

This will give you a 2x2 matrix that is the inverse of A.

main.m
inv_A =

   -2.0000    1.0000
    1.5000   -0.5000
51 chars
5 lines

You can verify that the matrix is the inverse by multiplying it with the original matrix and checking whether you get the identity matrix:

main.m
A * inv_A

ans =

    1.0000         0
         0    1.0000
60 chars
7 lines

This confirms that inv_A is the inverse of A.

gistlibby LogSnag