Java With Us
 Home   |   Tutorial   |   Programs

We will see how to perform matrix addition and subtraction in java. The two operations are similar except for a small difference.

A matrix can be represented in Java as a two dimensional array with the length of the 2D array equal to the number of rows of the matrix and the length of the sub arrays equal to the number of columns of the matrix. For example, a matrix of order 3*7 will be represented as a 2D array matrix[3][7]. A two level nested for loop will be used to read the input matrices from the keyboard. The outer loop counter, i ranges from 0 to the number of rows of the matrix while the inner loop counter, j ranges from 0 to the number of columns of the matrix. Within the inner loop, the input integers will be read using nextInt() method of the scanner class and stored at position [i][j] of the array.

Once the two matrices are read, we use a two level nested for loop with loop counters similar to the ones used for adding the matrices. The elements at [i][j] of the two input matrices are added and the result is stored at position [i][j] of the result matrix. For subtraction, the same procedure is followed except that we subtract the elements at [i][j] instead of adding them.

Finally, we print the matrices using a nested for loop. We wish to print the array in the form of a rectangular matrix. For this purpose, we use print() instead of println() for displaying the values within the inner loop. At the end of the inner loop, we use a println() statement without any arguments to move to the next line.

The complete Java program for addition of two matrices is given below.

import java.util.Scanner;

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = s.nextInt();
System.out.print("Enter number of columns: ");
int columns = s.nextInt();
int[][] a = new int[rows][columns];
int[][] b = new int[rows][columns];
System.out.println("Enter the first matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
b[i][j] = s.nextInt();
}
}
int[][] c = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The sum of the two matrices is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}

Here is a sample execution.

Enter number of rows: 2
Enter number of columns: 3
Enter the first matrix
3 4 7
1 8 4
Enter the second matrix
3 2 1
1 0 4
The sum of the two matrices is
6 6 8
2 8 8

For subtraction, the plus sign in line 'c[i][j] = a[i][j] + b[i][j];' should be changed to a minus sign.

In the above program, a lot of code was repetitive in nature. We have used the same code to read the two matrices. If both addition and subtraction are to be performed, the code for printing the matrices will also be duplicated twice. Instead of writing the same code a number of times, we can use methods. Given below is a modified version of the previous program which takes two input matrices A and B and performs three operations, A+B, A-B, B-A.

import java.util.Scanner;

public class Matrices {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();
System.out.println();
System.out.println("Enter first matrix");
System.out.println();
System.out.println("Enter second matrix");
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);
System.out.println();
System.out.println("A + B =");
printMatrix(sum);
System.out.println();
System.out.println("A - B =");
printMatrix(difference1);
System.out.println();
System.out.println("B - A =");
printMatrix(difference2);
}

public static int[][] readMatrix(int rows, int columns) {
int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = s.nextInt();
}
}
return result;
}

public static int[][] add(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 int[][] subtract(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) {
int rows = matrix.length;
int columns = matrix[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

Here is a sample output.

Enter number of rows: 2
Enter number of columns: 2

Enter first matrix
3 4
7 9

Enter second matrix
1 2
3 4

A + B =
4 6
10 13

A - B =
2 2
4 5

B - A =
-2 -2
-4 -5