ในบทความนี้ เราจะเข้าใจวิธีการเพิ่มเมทริกซ์สองตัวโดยใช้อาร์เรย์หลายมิติ เมทริกซ์มีการจัดเรียงแถวและคอลัมน์ขององค์ประกอบ เมทริกซ์ที่มี m แถวและ n คอลัมน์สามารถเรียกได้ว่าเป็นเมทริกซ์ขนาด m × n รายการแต่ละรายการในเมทริกซ์เรียกว่าองค์ประกอบและสามารถแสดงด้วย a[i][j] ซึ่งแสดงให้เห็นว่าองค์ประกอบ a มีอยู่ในแถว ith และคอลัมน์ jth
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
First matrix: 2 3 4 5 2 3 4 6 9 Second matrix: 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
อัลกอริทึม
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 and add the element at [i][j] position of the first matrix with the element at [i][j] position of the second matrix and store the value at [i][j] position of the resultant matrix. Step 5 - Display the result matrix Step 5 - Stop
ตัวอย่างที่ 1
ที่นี่ เราเชื่อมโยงการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
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
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
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