Perform Matrix Addition in Java
What you’ll learn
- Matrix addition rule
C[i][j] = A[i][j] + B[i][j]. - Adding matrices with nested loops in Java.
- 3x3 and 2x2 complete Java examples.
Prerequisites
2D arrays, nested loops, and same-dimension matrix rule.
- Declare and access 2D arrays like
matrix[i][j]. - Use nested loops to traverse rows and columns.
Math definition
For same-size matrices A and B, addition is C[i][j] = A[i][j] + B[i][j].
Intuition and examples
Add each matching cell independently; no cross-cell mixing.
Live preview
Algorithm
Check dimensions
Both matrices must have identical row and column counts.
Traverse cells
Use nested loops for rows and columns.
Add entrywise
Set result[i][j] = A[i][j] + B[i][j] for each cell.
📜 Pseudocode
Pseudocode
for i in 0..rows-1:
for j in 0..cols-1:
result[i][j] = A[i][j] + B[i][j]1
Add two 3x3 matrices
java
public class Main {
static void addMatrices(int[][] m1, int[][] m2, int[][] result) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = m1[i][j] + m2[i][j];
}
}
}
static void displayMatrix(int[][] matrix) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int[][] resultMatrix = new int[3][3];
addMatrices(matrix1, matrix2, resultMatrix);
System.out.println("Matrix 1:");
displayMatrix(matrix1);
System.out.println("\nMatrix 2:");
displayMatrix(matrix2);
System.out.println("\nResultant Matrix:");
displayMatrix(resultMatrix);
}
}2
Add two 2x2 matrices
java
public class Main {
static final int ROWS = 2;
static final int COLS = 2;
static void addMatrices(int[][] a, int[][] b, int[][] out) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
out[i][j] = a[i][j] + b[i][j];
}
}
}
static void printMatrix(String title, int[][] m) {
System.out.println(title);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{4, 3}, {2, 1}};
int[][] c = new int[ROWS][COLS];
addMatrices(a, b, c);
printMatrix("A", a);
System.out.println();
printMatrix("B", b);
System.out.println();
printMatrix("A + B", c);
}
}Optimization
Linear scan over entries is optimal; keep loops row-major for cache locality.
❓ FAQ
Only when row and column counts are the same for both matrices.
Usually with 2D arrays like int[][] matrix.
Yes. A + B = B + A entry-wise.
Same shape rule; subtract matching entries.
Adding m*n entries takes O(m*n) time.
Because every row-column position must be visited exactly once.
🔄 Input / output notes
The sample 3x3 matrices sum to all 10s in every position.
Edge cases
Dimensions
Mismatched sizes
Addition is undefined when row/column counts differ.
Overflow
Large entries
Use wider numeric types if element sums can exceed int range.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Add two m x n matrices | O(m * n) | O(1) (excluding output matrix) |
Summary
- Matrix addition is element-wise for same dimensions.
- Nested loops provide a direct
O(m * n)solution.
Did you know?
Matrix addition is entry-wise: (A+B)[i][j] = A[i][j] + B[i][j]. It is defined only when both matrices have the same dimensions.
8 people found this page helpful
