Table of Contents
In Java, we may combine two matrices by using the binary + operator. A matrix is frequently referred to as an array of arrays. Matrixes can be added, subtracted, and multiplied. Java application for combining two matrices – The Java code below demonstrates how to conduct two matrix addition in Java. We wrote the programme in three different methods. This is it,
- Using For Loop
- Using While
- Using Do-While
The addition of two matrices is possible if and only if both matrices are in the same order. In matrix addition, each term of one matrix is added to the term of the other matrix at the same place, i.e. the term in Matrix 1’s first row, the first column is added to the term in Matrix 2’s first row, the first column, and so on. A visual illustration is shown below.
Learn to code from industry experts! Enroll here
public class AddMatrices {
public static void main(String[] args) {
int rows = 2, columns = 3;
int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
int[][] secondMatrix = { {-4, 5, 3}, {5, 6, 3} };
// Adding Two matrices
int[][] sum = new int[rows][columns];
for(int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
// Displaying the result
System.out.println("Sum of two matrices is: ");
for(int[] row : sum) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
Output
Sum of two matrices is: -2 8 7 10 8 6
The two matrices, the first matrix, and second Matrix are stored in a 2d array in the preceding program. We’ve also specified the number of rows and columns, which we’ve saved in the variables rows and columns, respectively. Then, we create a new array named sum from the provided rows and columns. The addition of the specified matrices is stored in this matrix array. We add and save the result by looping over each index of both arrays. Finally, we use the for-each loop to cycle through each element in the sum array and output the elements.
Addition Of Two Matrices – Using For Loop
Only if two matrices are the same size may they be added. To store the matrix elements, use a double-dimensional array. Read the row and column numbers and populate the double-dimensional arrays mat1[][],mat2[][],res[][] with the identical row and column numbers. Using two for loops, load the initial matrix elements into the two-dimensional array mat1[][]. The letter I represent the row number, while the letter j represents the column index. Similarly, matrix 2 items are entered into mat2[][].
Using a for loop, add the two matrices.
- for i=0 to i<row
- for j=0 to j<col
- mat1[i][j] + mat2[i][j] and store it in to the matrix res at res[i][j] .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import java.util.Scanner;
class AddMatrix
{
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System.in);
System.out.println(“Enter the number of rows”);
row = in.nextInt();
System.out.println(“Enter the number columns”);
col = in.nextInt();
int mat1[][] = new int[row][col];
int mat2[][] = new int[row][col];
int res[][] = new int[row][col];
System.out.println(“Enter the elements of matrix1”);
for ( i= 0 ; i < row ; i++ )
{
for ( j= 0 ; j < col ;j++ )
mat1[i][j] = in.nextInt();
System.out.println();
}
System.out.println(“Enter the elements of matrix2”);
for ( i= 0 ; i < row ; i++ )
{
for ( j= 0 ; j < col ;j++ )
mat2[i][j] = in.nextInt();
System.out.println();
}
for ( i= 0 ; i < row ; i++ )
for ( j= 0 ; j < col ;j++ )
res[i][j] = mat1[i][j] + mat2[i][j] ;
System.out.println(“Sum of matrices:-“);
for ( i= 0 ; i < row ; i++ )
{
for ( j= 0 ; j < col ;j++ )
System.out.print(res[i][j]+“\t”);
System.out.println();
}
}
}
|
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Enter the number of rows
2
Enter the number  columns
2
Enter the elements of matrix1
1 1
1 1
Enter the elements of  matrix2
2 2
2 2
Sum of  matrices:–
3Â Â Â Â Â Â 3
3Â Â Â Â Â Â 3
|
Using While Loop
1) For insert values in the matrices mat1, mat2 using loops –
- While loop iterates until i <row
- while iterates until  j<column
- insert the value at  mat1[i][j] and increases the j value.
- increase the i value.
2) To add matrices –
- while loop iterates until i<row
- while loop iterates until j<column
- add  mat1[i][j] + mat2[i][j] and insert the result at res[i][j].
- increase the j value.
- increase the i value.
3)Â Print res[i][j] using two for loops then we will get the addition of two matrices.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import java.util.Scanner;
class AddMatrix
{
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System.in);
System.out.println(“Enter the number of rows”);
row = in.nextInt();
System.out.println(“Enter the number columns”);
col = in.nextInt();
int mat1[][] = new int[row][col];
int mat2[][] = new int[row][col];
int res[][] = new int[row][col];
System.out.println(“Enter the elements of matrix1”);
i=0;
while ( i < row)
{
j=0;
while(j < col)
{
mat1[i][j] = in.nextInt();
j++;
}
i++;
}
System.out.println(“Enter the elements of matrix2”);
i=0;
while ( i < row)
{
j=0;
while(j < col)
{
mat2[i][j] = in.nextInt();
j++;
}
i++;
}
i=0;
while ( i < row)
{
j=0;
while(j < col)
{
res[i][j] = mat1[i][j] + mat2[i][j] ;
j++;
}
i++;
}
System.out.println(“Sum of matrices:-“);
i=0;
while ( i < row)
{
j=0;
while(j < col)
{
System.out.print(res[i][j]+“\t”);
j++;
}
System.out.println();
i++;
}
}
}
|
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Enter the number of rows
3
Enter the number  columns
3
Enter the elements of matrix1
1 2 3
4 5 6
7 8 9
Enter the elements of  matrix2
9 8 7
6 5 4
3 2 1
Sum of  matrices:–
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
|
Using Do-While Loop
1) For the Addition of two matrices
- i=0
- do loop
- j=0
- do loop
- mat1[i][j] + mat2[i][j] and insert the result in to res[i][j].
- Increase the j value. checks the condition j<col.
- Increase the i value. Then checks the condition j<row.
2)Â Print res[][] then we will get the addition of two matrices.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import java.util.Scanner;
class AddMatrix
{
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System.in);
System.out.println(“Enter the number of rows”);
row = in.nextInt();
System.out.println(“Enter the number columns”);
col = in.nextInt();
int mat1[][] = new int[row][col];
int mat2[][] = new int[row][col];
int res[][] = new int[row][col];
System.out.println(“Enter the elements of matrix1”);
i=0;
do
{
j=0;
do
{
mat1[i][j] = in.nextInt();
j++;
}while(j < col);
i++;
} while ( i < row);
System.out.println(“Enter the elements of matrix2”);
i=0;
do
{
j=0;
do
{
mat2[i][j] = in.nextInt();
j++;
}while(j < col);
i++;
} while ( i < row);
i=0;
do
{
j=0;
do
{
res[i][j] = mat1[i][j] + mat2[i][j] ;
j++;
}while(j < col);
i++;
}while ( i < row);
System.out.println(“Sum of matrices:-“);
i=0;
do
{
j=0;
do
{
System.out.print(res[i][j]+“\t”);
j++;
}while(j < col);
System.out.println();
i++;
}while(i< row);
}
}
|
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Enter the number of rows
3
Enter the number of columns
3
Enter the elements of matrix1
1 1 1
1 1 1
1 1 1
Enter the elements of  matrix2
9 9 9
9 9 9
9 9 9
Sum of matrices:–
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
10Â Â Â Â Â Â 10Â Â Â Â Â Â 10
|