Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java คูณเมทริกซ์ด้วย Multi-Dimensional Arrays


ในบทความนี้ เราจะเรียนรู้วิธีการคูณเมทริกซ์ 2 ตัวด้วย Multi-Dimensional Arrays ใน Java เมทริกซ์มีการจัดเรียงองค์ประกอบเป็นแถวและคอลัมน์ เมทริกซ์ที่มี m แถวและ n คอลัมน์ เรียกว่าเมทริกซ์ m × n องค์ประกอบแต่ละตัวในเมทริกซ์เรียกว่า element และแทนด้วย a[i][j] ซึ่งหมายถึงองค์ประกอบ a อยู่ที่แถวที่ i และคอลัมน์ที่ j

ตัวอย่างการทำงาน

สมมติข้อมูลนำเข้า (Input) คือ:

First matrix: 2 3 4 5 2 3 4 6 9 Second matrix: 1 5 3 5 6 3 8 1 5

ผลลัพธ์ที่ต้องการ (Output) คือ:

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

อัลกอริทึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศเมทริกซ์จำนวนเต็ม 3 ตัว คือ input_matrix_1, input_matrix_2 และ resultant_matrix
ขั้นตอนที่ 3 - กำหนดค่าให้กับเมทริกซ์
ขั้นตอนที่ 4 - วนลูปผ่านแต่ละองค์ประกอบของทั้ง 2 เมทริกซ์ด้วย for-loop คูณองค์ประกอบ [i][j] ของเมทริกซ์แรกกับแต่ละองค์ประกอบของแถวในเมทริกซ์ที่สอง แล้วบวกกัน เก็บไว้ที่ [i][j] ของ resultant matrix ทำซ้ำสำหรับทุกองค์ประกอบ
ขั้นตอนที่ 5 - แสดงผลลัพธ์
ขั้นตอนที่ 6 - จบการทำงาน

ตัวอย่างที่ 1: เขียนโค้ดทั้งหมดในฟังก์ชัน 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(); 
        } 
    } 
}

ผลลัพธ์

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

ตัวอย่างที่ 2: ใช้ฟังก์ชันแยก (Object-Oriented Style)

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

ผลลัพธ์

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