Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để nhân lên ma trận bằng cách sử dụng mảng đa chiều

Trong bài này, chúng ta sẽ hiểu cách nhân với ma trận bằng cách sử dụng mảng nhiều chiều. Ma trận có sự sắp xếp theo hàng và cột của các phần tử của nó. Ma trận có m hàng và n cột có thể được gọi là ma trận m × n.

Các mục riêng lẻ trong ma trận được gọi là phần tử và có thể được biểu diễn bằng [i] [j], điều này gợi ý rằng phần tử a có trong hàng thứ i và cột thứ j.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

First matrix:
2 3 4
5 2 3
4 6 9

Second matrix:
1 5 3
5 6 3
8 1 5

Đầu ra mong muốn sẽ là -

The product of two matrices is:
49 32 35
39 40 36
106 65 75

Thuật toán

Step 1 - START
Step 2 - Declare three integer matrices namely input_matrix_1, input_matrix_1 and resultant_matrix
Step 3 - Define the values.
Step 4 - Iterate over each element of the both the matrices using for-loop, multiply the element at [i][j] position of the first matrix with each element of the row of the second matrix and add the values, store the value at [i][j] position of the resultant matrix. Repeat this for each element of the first matrix.
Step 5 - Display the result
Step 5 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

public class MultiplyMatrices {
   public static void main(String[] args) {
      int matrix_size = 3;
      int[][] input_matrix_1 = {
         {2, 3, 4},
         {5, 2, 3},
         {4, 6, 9}
      };
      System.out.println("The first matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix_1[i][j] + " ");
         }
         System.out.println();
      }
      int[][] input_matrix_2 = {
         {1, 5, 3},
         {5, 6, 3},
         {8, 1, 5}
      };
      System.out.println("The second matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix_2[i][j] + " ");
         }
      System.out.println();
   }
   int[][] resultant_matrix = new int[matrix_size][matrix_size];
   for(int i = 0; i < matrix_size; i++) {
      for (int j = 0; j < matrix_size; j++) {
         for (int k = 0; k < matrix_size; k++) {
            resultant_matrix[i][j] += input_matrix_1[i][k] * input_matrix_2[k][j];
         }
      }
   }
   System.out.println("\n The product of two matrices is: ");
   for(int[] row : resultant_matrix) {
      for (int column : row) {
         System.out.print(column + " ");
      }
      System.out.println();
      }
   }
}

Đầu ra

The first matrix is defined as:
2 3 4
5 2 3
4 6 9

The second matrix is defined as:
1 5 3
5 6 3
8 1 5

The product of two matrices is:
49 32 35
39 40 36
106 65 75

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

public class MultiplyMatrices {
   static int matrix_size = 3;
   static void multiply(int input_matrix_1[][], int input_matrix_2[][]){
      int[][] resultant_matrix = new int[matrix_size][matrix_size];
      for(int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            for (int k = 0; k < matrix_size; k++) {
               resultant_matrix[i][j] += input_matrix_1[i][k] * input_matrix_2[k][j];
            }
         }
      }
      System.out.println("\n The product of two matrices is: ");
      for(int[] row : resultant_matrix) {
         for (int column : row) {
            System.out.print(column + " ");
         }
         System.out.println();
      }
   }
   public static void main(String[] args) {
      int matrix_size = 3;
      int[][] input_matrix_1 = {
         {2, 3, 4},
         {5, 2, 3},
         {4, 6, 9}
      };
      System.out.println("The first matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix_1[i][j] + " ");
         }
      System.out.println();
      }
      int[][] input_matrix_2 = { {1, 5, 3},
         {5, 6, 3},
         {8, 1, 5}
      };
      System.out.println("The second matrix is defined as: ");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(input_matrix_2[i][j] + " ");
         }
         System.out.println();
      }
      multiply(input_matrix_1, input_matrix_2);
   }
}

Đầu ra

The first matrix is defined as:
2 3 4
5 2 3
4 6 9

The second matrix is defined as:
1 5 3
5 6 3
8 1 5

The product of two matrices is:
49 32 35
39 40 36
106 65 75