How to find the rank of a matrix in Matlab and Octave
To calculate the rank of a matrix on Matlab / Octave we use the function rank()
rank(M)
The M parameter is an array.
The rank() function calculates the rank of the array.
What is rank? Rank is the number of linearly independent column vectors in the matrix. It is the highest order submatrix with non-zero determinant.
Example
Define a 3x4 matrix with three rows and four columns.
>> M=[1 0 3 4 ; 1 0 2 8 ; 3 5 2 1]
M =
1 0 3 4
1 0 2 8
3 5 2 1
Calculate the rank of the matrix M using the function rank()
>> rank(M)
ans = 3
The rank of the matrix is 3.
The highest-order submatrix with non-zero determinant is of order three
Verify. A submatrix of M is the square matrix obtained with the first three columns. It is a square submatrix of order 3 with non-zero determinant. $$ \det \begin{pmatrix} 1 & 0 & 3 \\ 1 & 0 & 2 \\ 3 & 5 & 2 \end{pmatrix} = 5 \ne 0 $$