# Nullity and Rank of a Matrix in C++

Hello there! In this article, we will learn to find the Nullity and Rank of a matrix in C++.

#### Nullspace, Nullity, and Rank of a Matrix

If **M** is a * p x q* matrix, then the Nullspace of the matrix is the set of all n-dimensional column vectors

**N**such that

**MN=0.**

The dimension of the null space is called the nullity of the matrix.

The dimension of the row space is called the rank of a matrix.

Rank + Nullity = Number of columns of a Matrix.

Example :

[0,0,0]

[0,0,1]

[1,0,0]

The Rank of the above matrix is 2 as there are two independent rows in the matrix i.e, row 2 and row 3. Therefore, the Nullity of the matrix will be the **number of columns in the matrix-Rank** which will be 1 for the above matrix.

## Finding Rank and Nullity of Matrix in C++

#### 1. Taking Input

First, Take the number of rows and columns as input and store them into the variables **n** and **m** respectively.

Next, take the matrix values from the input and store them in the declared matrix called **mat[n][m]**.

#### 2. Making the matrix ready for row transformations

Now we have to make our matrix ready for the row reducing transformations.

If **mat[0][0]** is **zero**, we use the **Z** variable initialized with **-1** and two for loops iterating over columns and rows to find the respective row number to be exchanged with the first row.

**Z** value doesn’t change if there is no such row else it stores the value of the row to be exchanged. In case **Z** is not **-1** we exchange the rows.

If all the values in the first column are **0** then we go for the second column values in the respective rows and repeat the process.

#### 3. Performing row transformations to achieve echelon form

Next, we convert the matrix to its row echelon form by performing row transformations.

It requires 3 for loops to perform the row transformation. First for loop will iterate **k** from **0 **th row to **n-2 **th row

and second for loop will iterate **j** from **k+1 **th row to **n-1 **th row.

Store **mat[k][k]** into **p **variableĀ and **mat[i][k]** into **q** variable to use in the formula for performing row transformations.

Next, iterate the variable **j** from **0** to **m-1** and perform row transformation using the formula.

**mat[i][j]=p*mat[i][j] – q*mat[k][j]**

Now,we shall find the rank of the matrix.

#### 4. Finding rank of the matrix

Initialize the variable **Z** to 0(We use Z here to find the number of independent rows in the matrix.)

Use two for loops, the first one iterates over the rows and the second one over the columns. After the first for loop, we use a variable **c** to count the number of zeros in each row. if **c** equals **m** we increment the **z**.

Now, the Rank of the matrix can be found by using the formula **n-Z**. So store the value of **n-Z** into **R** which is the rank of the matrix.

Now store the value of **m-R** into the **N** variable which represents the Nullity value.

Finally, we print the values of Rank and Nullity.

### C++ Program to find Nullity and Rank of a Matrix

#include<iostream> using namespace std; int main() { int mat[10][10],i,j,n,k,m,r,N,z,R,p,q,v; cin>>n>>m; for(i=0;i<n;i++){ for(j=0;j<m;j++){ cin>>mat[i][j]; } } if(mat[0][0]==0){ for(j=0;j<m;j++){ z=-1; for(i=1;i<n;i++){ if(mat[i][j]!=0){ z=i; break; } } if(z!=-1){ for( v=0;v<m;v++){ int t=mat[0][v]; mat[0][v]=mat[i][v]; mat[i][v]=t; } break; } } } for(k=0;k<n-1;k++){ for(i=k+1;i<n;i++){ p=mat[k][k]; q=mat[i][k]; for(j=0;j<m;j++){ mat[i][j]=p*mat[i][j] - q*mat[k][j]; } } } z=0; for(i=0;i<n;i++){ int c=0; for(j=0;j<m;j++){ if(mat[i][j]==0){ c++; } } if(c==m){ z++; } } R=n-z; N=m-R; cout<<"Rank = "<<R<<endl; cout<<"Nullity = "<<N<<endl; }

#### Input

4 4 0 0 -1 0 0 0 -1 0 1 0 0 0 1 0 1 0

#### Output

Rank = 2 Nullity = 2

Hurrah! we have successfully learned how to find the rank and nullity of a matrix using C++.

Thank you for reading the article, I hope you have found it useful.

Also do check out our other related articles below :

## Leave a Reply