ในบทความนี้ เราจะเรียนรู้วิธีการบวกเมทริกซ์ 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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14
อัลกอริทึม
ขั้นตอนที่ 1 - เริ่มต้น ขั้นตอนที่ 2 - ประกาศเมทริกซ์จำนวนเต็ม 3 ตัว คือ input_matrix_1, input_matrix_2 และ resultant_matrix ขั้นตอนที่ 3 - กำหนดค่าให้กับเมทริกซ์ ขั้นตอนที่ 4 - วนลูปผ่านแต่ละองค์ประกอบของทั้ง 2 เมทริกซ์ด้วย for-loop บวกองค์ประกอบ [i][j] ของเมทริกซ์แรกกับ [i][j] ของเมทริกซ์ที่สอง เก็บไว้ที่ [i][j] ของ resultant matrix ขั้นตอนที่ 5 - แสดงเมทริกซ์ผลลัพธ์ ขั้นตอนที่ 6 - จบการทำงาน
ตัวอย่างที่ 1: เขียนโค้ดทั้งหมดในฟังก์ชัน main
public class AddMatrices {
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: \n");
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: \n");
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++) {
resultant_matrix[i][j] = input_matrix_1[i][j] + input_matrix_2[i][j];
}
}
System.out.println("The sum of the 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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14
ตัวอย่างที่ 2: ใช้ฟังก์ชันแยก (Object-Oriented Style)
public class AddMatrices {
static int matrix_size = 3;
static void add(int input_matrix_1[][], int input_matrix_2[][]){
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++) {
resultant_matrix[i][j] = input_matrix_1[i][j] + input_matrix_2[i][j];
}
}
System.out.println("\nThe sum of the 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[][] 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("\nThe second matrix is defined as: ");
add(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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14