How to Check if the Matrix is Diagonal Matrix in Java?


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

A matrix having non-zero elements only in the main diagonal (running from the upper left to the lower right) is called as diagonal matrix.

As per the problem statement we have to a matrix and we have to check whether that given matrix is a diagonal 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

Given matrix =

21 	0	0
0	25	0
0	0	29

The given matrix is a diagonal matrix because the main diagonal only contains non-zero values and other elements are zeros.

Instance-2

Given matrix =

121 	0	0	432
0	567	0	0
0	0	229	0
176	0	0	343

The given matrix is not a diagonal matrix because with main diagonal some other elements are also containing non-zero values.

Instance-3

Given matrix =

1 	0	0	0	0
0	7	0	0	0
0	0	13	0	0
0	0	0	19	0
0	0	9	0	25

The given matrix is a diagonal matrix because the main diagonal only contains non-zero values and other elements are zeros.

Algorithm

Algorithm-1

  • Step-1 − Define the matrix as a 2D array.

  • Step-2 − Check if the matrix is square. If not, set the checkDiagonal flag to false.

  • Step-3 − Loop over all elements in the matrix. If an off-diagonal element is non-zero, set the checkDiagonal flag to false and break out of both loops using the break statement.

  • Step-4 − Print whether the matrix is diagonal or not based on the value of the checkDiagonal flag.

Algorithm-2

  • Step-1 − Define the matrix as a 2D array.

  • Step-2 − Check if the matrix is square. If not, set the checkDiagonal flag to false.

  • Step-3 − Use streams to check if any off-diagonal element is non-zero. If so, set the checkDiagonal flag to false.

  • Step-4 − Use the isEmpty() method to check if the stream is empty. If so, the matrix is diagonal.

  • Step-5 − Print whether the matrix is diagonal or not based on the value of the checkDiagonal flag.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Nested for Loop

  • By Using Java Streams

Let’s see the program along with its output one by one.

Approach-1: By Using Nested for Loop

In this approach, matrix elements will be initialized in the program. Then as per the algorithm using nested for loop check whether the matrix is a diagonal matrix or not.

Example

public class Main {
   public static void main(String[] args) {
      // Define the matrix
      int[][] inputMatrix = {{1, 0, 0}, {0, 5, 0}, {0, 0, 7}};
      
      // declare a variable to store the boolean values
      boolean checkDiagonal = true;
      if (inputMatrix.length != inputMatrix[0].length) {
         checkDiagonal = false;
      } else {
         for (int i = 0; i < inputMatrix.length; i++) {
            for (int j = 0; j < inputMatrix[0].length; j++) {
               if (i != j && inputMatrix[i][j] != 0) {
                  checkDiagonal = false;
                  break;
               }
            }
            if (!checkDiagonal) {
               break;
            }
         }
      }
      
      // Print the result
      if (checkDiagonal) {
         System.out.println("The matrix is a diagonal matrix");
      } else {
         System.out.println("The matrix is not a diagonal matrix");
      }
   }
}

Output

The matrix is a diagonal matrix

Approach-2: By Using Java Streams

In this approach, matrix elements will be initialized in the program. Then as per the algorithm using java streams check whether the matrix is a diagonal matrix or not.

Example

import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
   public static void main(String[] args) {
      
      // Define the matrix
      int[][] inputMatrix = {{1, 0, 0}, {0, 5, 5}, {0, 0, 7}};

      // declare a variable to store the boolean values
      boolean checkDiagonal = true;
      if (inputMatrix.length != inputMatrix[0].length) {
         checkDiagonal = false;
      } else {
         checkDiagonal = IntStream.range(0, inputMatrix.length)
            .flatMap(i -> IntStream.range(0, inputMatrix[0].length)
               .filter(j -> i != j && inputMatrix[i][j] != 0))
            .findFirst()
            .isEmpty();
      }

      // Print the result
      if (checkDiagonal) {
         System.out.println("The given matrix is a diagonal matrix");
      } else {
         System.out.println("The given matrix is not a diagonal matrix");
      }
   }
}

Output

The given matrix is not a diagonal matrix 

In this article, we explored different approaches to check if the matrix is a diagonal matrix or not by using Java programming language.

Updated on: 04-May-2023

473 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements