Open In App

Java Program for Identity Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

Introduction to Identity Matrix :

 The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1’s and all other elements are zeros. In the below image, every matrix is an Identity Matrix. 
 

In linear algebra, this is sometimes called as a Unit Matrix, of a square matrix (size = n x n) with ones on the main diagonal and zeros elsewhere. The identity matrix is denoted by “ I “. Sometimes U or E is also used to denote an Identity Matrix. 
A property of the identity matrix is that it leaves a matrix unchanged if it is multiplied by an Identity Matrix.

Examples:  

Input  : 2
Output : 1 0
         0 1

Input :  4
Output : 1 0 0 0
         0 1 0 0
         0 0 1 0
         0 0 0 1
The explanation is simple. We need to make all
the elements of principal or main diagonal as 
1 and everything else as 0.

Program to print Identity Matrix : 
The logic is simple. You need to the print 1 in those positions where row is equal to column of a matrix and make all other positions as 0. 

Java




// Java program to print Identity Matrix
class GFG {
     
    static int identity(int num)
    {
        int row, col;
          
        for (row = 0; row < num; row++)
        {
            for (col = 0; col < num; col++)
            {
                // Checking if row is equal to column
                if (row == col)
                    System.out.print( 1+" ");
                else
                    System.out.print( 0+" ");
            }
            System.out.println();
        }
        return 0;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int size = 5;
        identity(size);
    }
}
 
/*This code is contributed by Nikita tiwari.*/


Output: 

1  0  0  0  0  
0  1  0  0  0  
0  0  1  0  0  
0  0  0  1  0  
0  0  0  0  1  

Time complexity: O(n2) where n is the number of rows and columns of a given matrix.

Auxiliary Space: O(1)

Program to check if a given square matrix is Identity Matrix : 

Java




// Java program to check if a given
// matrix is identity
class GFG {
     
    int MAX = 100;
  
    static boolean isIdentity(int mat[][], int N)
    {
        for (int row = 0; row < N; row++)
        {
            for (int col = 0; col < N; col++)
            {
                if (row == col && mat[row][col] != 1)
                    return false;
                else if (row != col && mat[row][col] != 0)
                    return false;
            }
        }
        return true;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int N = 4;
        int mat[][] = {{1, 0, 0, 0},
                       {0, 1, 0, 0},
                       {0, 0, 1, 0},
                       {0, 0, 0, 1}};
         
        if (isIdentity(mat, N))
           System.out.println("Yes ");
        else
           System.out.println("No ");
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/


Output:

Yes

Time complexity: O(n2) where n is number of rows and columns of given matrix

Auxiliary Space: O(1)
 



Last Updated : 19 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads