Matrices: Matrices in MATLAB

Theory Row reduction and determinant

MATLAB provides row and column operations to compute reduction processes in matrices numerically, but calculate the reduced row echelon form of a matrix with the command rref. This allows you to perform the method of row reduction for calculating an inverse matrix.

Computing the inverse of a matrix

>> A = [3 1 -4; -1 0 2; 1 1 -1]    % invertible matrix
A =
     3     1    -4
    -1     0     2
     1     1    -1
>> AI = [A eye(3)]    % augmented matrix
AI =
     3     1    -4     1     0     0
    -1     0     2     0     1     0
     1     1    -1     0     0     1
>> rref(AI)    % reduced row echelon form
ans =
     1     0     0     2     3    -2
     0     1     0    -1    -1     2
     0     0     1     1     2    -1
>> ans(:, 4:6)  % submatrix equal to inverse of A
ans =
     2     3    -2
    -1    -1     2
     1     2    -1
>> A^(-1)
ans =
    2.0000    3.0000   -2.0000
   -1.0000   -1.0000    2.0000
    1.0000    2.0000   -1.0000

The determinant of the matrix \(A\) in the above example can be calculated with the command det, and the result shows that the matrix is invertible. We give below also an example of a non-invertible matrix because the determinant is equal to 0 and the reduced echelon form is not equal to an identity matrix. If you're still trying to calculate the inverse, you actually get a result with a matching warning. In addition, be always on guard for numerical rounding errors in calculations.

Determinant of a matrix

>> A         % invertible matrix
A =
     3     1    -4
    -1     0     2
     1     1    -1
>> det(A)    % determinant of A
ans =
   -1.0000
>> B = [1 -2 -2; 1 14 14; -4 -24 -24]  % non-invertible matrix
B =
     1    -2    -2
     1    14    14
    -4   -24   -24
>> det(B)    % determinant of B
ans =
0
>> rank(B) % rang van B
ans =
2
>> rref(B) % reduced row echelon form of B ans = 1 0 0 0 1 1 0 0 0 >> B^(-1) Warning: Matrix is singular to working precision. > In matlab.internal.math.mpower.viaMtimes (line 35) ans = Inf Inf Inf Inf Inf Inf Inf Inf Inf
About us ⋅ Privacy ⋅ Terms and conditions
Copyright © 2022 SOWISO B.V.