Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java nhân hai ma trận dùng mảng đa chiều

Giới thiệu

Trong bài viết này, chúng ta sẽ tìm hiểu cách nhân hai ma trận sử dụng mảng đa chiều. Ma trận có sự sắp xếp hàng và cột. Ma trận m hàng n cột gọi là ma trận m × n. Các phần tử trong ma trận được biểu diễn bởi a[i][j] (phần tử a ở hàng i, cột j).

Minh họa

Đầu vào:

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:

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

Thuật toán

  1. Bắt đầu
  2. Khai báo 3 ma trận: input_matrix_1, input_matrix_2, resultant_matrix
  3. Khởi tạo giá trị
  4. Duyệt các phần tử bằng 3 vòng lặp for lồng nhau: nhân phần tử [i][k] của ma trận 1 với [k][j] của ma trận 2, cộng dồn vào [i][j] của ma trận kết quả
  5. Hiển thị kết quả
  6. Kết thúc

Ví dụ 1: Triển khai 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: Tách hàm static (OOP)

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