Giới thiệu
Trong bài viết này, chúng ta sẽ tìm hiểu cách cộng 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ử đượ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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14
Thuật toán
- Bắt đầu
- Khai báo 3 ma trận:
input_matrix_1,input_matrix_2,resultant_matrix - Khởi tạo giá trị
- Duyệt từng phần tử bằng 2 vòng lặp
for, cộng phần tử [i][j] của 2 ma trận, lưu vào [i][j] của ma trận kết quả - Hiển thị ma trận kết quả
- Kết thúc
Ví dụ 1: Triển khai trong hàm 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();
}
}
}
Đầ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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14
Ví dụ 2: Tách hàm static (OOP)
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);
}
}
Đầ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 sum of the two matrices is: 3 8 7 10 8 6 12 7 14