如何在Java中檢查矩陣是否為單位矩陣?


矩陣不僅僅是資料元素的集合,它以二維的矩形佈局排列。在Java中,一個二維陣列可以被視為一個矩陣。

單位矩陣是一個方陣,其主對角線元素均為1,其餘元素均為0。根據問題陳述,我們必須檢查給定的矩陣是否為單位矩陣。

讓我們深入研究本文,瞭解如何使用Java程式語言來實現。

舉幾個例子

示例1

Suppose we have a matrix
   | 1 0 0 |
A= | 0 1 0 |
   | 0 0 1 |

主對角線上為1,其餘為0。因此它是一個單位矩陣。

示例2

Suppose we have a matrix
   | 1 0 0 |
A =| 0 1 1 |
   | 0 0 1 |

主對角線上為1,但還有一個非對角線元素為1。因此它不是一個單位矩陣。

示例3

Suppose we have a matrix
    | 1 0 0 0|
A = | 0 0 1 0|
    | 0 0 1 0|
    | 0 0 0 1|

主對角線上不全是1,其餘也不全是0。因此它不是一個單位矩陣。

演算法

步驟1 - 儲存矩陣。

步驟2 - 現在呼叫檢查函式,該函式遍歷矩陣並檢查行==列的位置是否為1,其餘位置是否為0。

步驟3 - 列印結果。

多種方法

我們提供了多種方法的解決方案。

  • 使用矩陣的靜態初始化

  • 使用矩陣的動態初始化

讓我們逐一檢視程式及其輸出。

方法1:使用矩陣的靜態初始化

在這種方法中,矩陣在程式中初始化,然後我們檢查矩陣的主對角線是否為1,其餘元素是否為零。

示例

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;
   }
}

輸出

The matrix is an Identity matrix.

方法2:使用矩陣的動態初始化

在這種方法中,我們要求使用者輸入他們想要的尺寸的矩陣,然後檢查該矩陣是否為單位矩陣。

示例

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;
   }
}

輸出

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.

在本文中,我們探討了使用Java程式語言檢查矩陣是否為單位矩陣的不同方法。

更新於:2023年3月6日

1K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告