What is the rank of a matrix?
The rank of a matrix A is the maximal number of linearly independent columns of A. This is, in turn, equal to the maximal number of linearly independent rows of a matrix. For example, let’s say A is a matrix that has 3 rows and 3 columns. And the third column of the matrix is a linear combination of the first two rows. So, the maximal number of independent columns of the matrix is 2. So, the rank of the matrix will be 2.
For example, let’s consider the following matrix:
Now, if we subtract the second column from the first column of the matrix, we will get the third column. So, the third column is a linear combination of the first two columns of the matrix. Hence, the rank of the matrix is 2.
Now, a matrix A is said to have full rank if the rank of the matrix is the maximum for the given dimension of the matrix. And that will be the minimum of the number of rows and columns of the matrix. For example, if the number of rows is more than the number of columns, then the full rank will be equal to the number of columns of the matrix.
If a matrix does not have full rank, then the matrix is said to be a rank-deficient matrix. And the rank deficiency of a matrix is equal to the difference between the lesser of the number of rows or columns of the matrix and the rank of the matrix.
How to calculate the rank of a matrix using Python?
We can use the following Python code to calculate the rank of a matrix.
import numpy from numpy.linalg import matrix_rank A = numpy.random.randint(low=1, high=10, size=(3,3)) print("Matrix A: \n", A) r = matrix_rank(A) print("Rank of the matrix A: ", r)
Here, we are first creating a matrix A that contains random integers. We are using the numpy.random.randint() function for that purpose. The generated random integers will be within the range [low, high).
Now, we are using the matrix_rank() function from the numpy.linalg module to calculate the rank of the matrix using NumPy.
The output of the mentioned program will be:
Matrix A: [[8 8 4] [2 7 9] [4 5 5]] Rank of the matrix A: 3
0 Comments