
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Check if a Matrix is Identity Matrix or not in Java?
Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
Identity matrix is a square matrix which has all 1s as its principal diagonal elements and rest are 0s. As per the problem statement we have to0 check if the given matrix is Identity matrix or not.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose we have a matrix | 1 0 0 | A= | 0 1 0 | | 0 0 1 |
We have 1’s at primary diagonal and rest 0’s. Hence it is an identity matrix
Instance-2
Suppose we have a matrix | 1 0 0 | A =| 0 1 1 | | 0 0 1 |
We have 1’s at primary diagonal but there is another 1 as non-diagonal element. Hence it is not an identity matrix
Instance-3
Suppose we have a matrix | 1 0 0 0| A = | 0 0 1 0| | 0 0 1 0| | 0 0 0 1|
We do not have 1’s at primary diagonal and rest 0’s. Hence it is not an identity matrix
Algorithm
Step 1 − Store the matrix.
Step 2 −Now call the check function that traverses the matrix and checks if the locations where rows==columns have 1s and rest are 0s.
Step 3 −Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix
By Using Dynamic Initialization of Matrix
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix
In this approach, matrix is initialized in the program, then we check if the matrix has 1 at its principal diagonal and rest elements are zero.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { // Matrix to be checked int mat[][] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, }; // Print results if (identityMatCheck(mat)) System.out.println("The matrix is an identity matrix."); else System.out.println("The matrix is not an identity matrix."); } //user defined method static boolean identityMatCheck(int mat[][]) { // Traverses the matrix for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { // Checks if the principal diagonal elemets are 1 if (i == j && mat[i][j] != 1) return false; // Checks if the rest elements are 0 else if (i != j && mat[i][j] != 0) return false; } } return true; } }
Output
The matrix is an Identity matrix.
Approach-2: By Using Dynamic Initialization of Matrix
In this approach, we ask the user to enter a matrix of their desired size, and and then check if the matrix is identity matrix or not.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Enter number of matrix rows-"); Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int mat[][] = new int[size][size]; // Enter Matrix Elements System.out.println("Enter the matrix elements"); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { mat[i][j] = sc.nextInt(); } } // Print results if (identityMatCheck(mat)) System.out.println("The matrix is an identity matrix."); else System.out.println("The matrix is not an identity matrix."); } static boolean identityMatCheck(int mat[][]) { // Traverses the matrix for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { // Checks if the principal diagonal elemets are 1 if (i == j && mat[i][j] != 1) return false; // Checks if the rest elemets are 0 else if (i != j && mat[i][j] != 0) return false; } } return true; } }
Output
Enter number of matrix rows- 4 Enter the matrix elements 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 The matrix is an identity matrix.
In this article, we explored different approaches to check if the matrix is an identity matrix or not by using Java programming language.
- Related Articles
- Check if a Matrix is Markov Matrix or not in Java
- Check if a Matrix is Involutory or not in Java?
- Program to check if a matrix is Binary matrix or not in C++
- Swift program to check if a given square matrix is an Identity Matrix
- Check if a given matrix is sparse or not in C++
- Check if a given matrix is Hankel or not in C++
- How to check if a matrix is invertible or not in R?
- C Program to check if matrix is singular or not
- JavaScript program to check if a given matrix is sparse or not
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- How to check in R whether a matrix element is present in another matrix or not?
- Check given matrix is magic square or not in C++
- C++ code to check given matrix is good or not
- Find if given matrix is Toeplitz or not in C++
- Check if a Matrix is Invertible in C++
