Implementation
To implement matrix addition in Java, we can use nested loops to iterate through the rows and columns of the matrices. Here's the step-by-step implementation:
- Create a new matrix called "result" with the same dimensions as the input matrices to store the sum.
- Use a nested loop to iterate through each element of the matrices:
- The outer loop iterates through the rows.
- The inner loop iterates through the columns.
- Add the corresponding elements of the input matrices and store the sum in the "result" matrix.
- Return the "result" matrix.
Let’s see the java program for the same :
Java
public class MatrixAddition {
public static void main(String[] args) {
int[][] A = {{1, 2}, {3, 4}};
int[][] B = {{5, 6}, {7, 8}};
int[][] result = addMatrices(A, B);
System.out.println("Matrix A:");
printMatrix(A);
System.out.println("Matrix B:");
printMatrix(B);
System.out.println("Result:");
printMatrix(result);
}
public static int[][] addMatrices(int[][] A, int[][] B) {
int rows = A.length;
int columns = A[0].length;
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
System.out.println();
}
}

You can also try this code with Online Java Compiler
Run Code
In this program :
- We define the main method, which is the entry point of the program.
- We create two matrices, A and B, as 2D arrays.
- We call the addMatrices method, passing A and B as arguments, and store the result in the result matrix.
- We print matrices A, B, and the result matrix using the printMatrix method.
The addMatrices method takes two matrices A and B as input and returns their sum:
- We determine the number of rows and columns based on the dimensions of matrix A.
- We create a new matrix called result with the same dimensions as the input matrices.
- We use nested loops to iterate through each element of the matrices and add the corresponding elements of A and B, storing the sum in the result matrix.
- Finally, we return the result matrix.
The printMatrix method takes a matrix as input and prints its elements in a formatted manner.
Output :
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
Result:
6 8
10 12
Time and Space Complexity:
Let's analyze the time and space complexity of the matrix addition operation.
Time Complexity
- To add two matrices of size n x m, we need to iterate through each element of the matrices.
- The nested loops in the `addMatrices` method run for n rows and m columns.
- Therefore, the time complexity of matrix addition is O(n * m), where n is the number of rows and m is the number of columns.
- In the worst case, when the matrices are square (i.e., n = m), the time complexity becomes O(n^2).
Space Complexity
- The space complexity of matrix addition depends on the size of the input matrices and the resulting matrix.
- We create a new matrix called `result` to store the sum of the input matrices.
- The `result` matrix has the same dimensions as the input matrices, i.e., n x m.
- Therefore, the space complexity of matrix addition is O(n * m), where n is the number of rows and m is the number of columns.
- In the worst case, when the matrices are square (i.e., n = m), the space complexity becomes O(n^2).
It's important to note that the time and space complexity of matrix addition grows quadratically with the size of the matrices. For larger matrices, the operation can become computationally expensive.
However, matrix addition is still considered an efficient operation compared to more complex matrix operations like matrix multiplication, which has a time complexity of O(n^3) for square matrices.
In practice, the efficiency of matrix addition can be improved by using techniques like parallel processing or optimized libraries for matrix operations, especially when dealing with large matrices.
Frequently Asked Questions
Can we add matrices of different sizes?
No, matrix addition is only possible when the matrices have the same dimensions, i.e., the same number of rows and columns.
What happens if we add a scalar value to a matrix?
Adding a scalar value to a matrix results in a new matrix where the scalar is added to each element of the original matrix.
Is matrix addition commutative?
Yes, matrix addition is commutative, meaning A + B = B + A, where A and B are matrices of the same size.
Conclusion
In this article, we learned about matrix addition in Java. We discussed the concept of adding two matrices by adding their corresponding elements. We provided an example to show the implementation of matrix addition using nested loops. Additionally, we discussed the time and space complexity of the matrix addition operation, which is O(n * m) for matrices of size n x m. Matrix addition is a fundamental operation in mathematics and computer science and has various applications in fields like linear algebra, image processing, and scientific computations.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.