Subtract Two Matrices in Java



For two given matrix each of size m×n, write a Java program to subtract them. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m×n matrix. Individual entries in the matrix are called element and can be represented by a[i][j] which means that the element a is present at the ith row and jth column.

Note that two matrix can be subtracted if and only if the number of rows and columns of each matrix are equal. Now, let's understand the problem statement with an example scenario ?

Example Scenario:

Input 1: matrix1 = {{2, 3, 4}, {5, 2, 3}, {4, 6, 9}}
Input 2: matrix2 = {{1, 5, 3}, {5, 6, 3}, {8, 1, 5}}
Output: res = {{1, -2, 1}, {0, -4, 0}, {-4, 5, 4}}  

Using Nested for Loop

In this approach, iterate over each element of the both matrices using outer for-loop and subtract the element at [i][j] position of the first matrix with the element at [i][j] position of the second matrix using inner for-loop and store the value at [i][j] position of the resultant matrix.

Example

In this Java program, we use the nested for loop to subtract two matrix.

public class Tester {
   public static void main(String args[]) {
      //matrix 1
      int a[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 3, 4, 5 } };
      //matrix 2
      int b[][] = { { 1, 3, 4 }, { 2, 4, 3 }, { 1, 2, 4 } };
      //result matrix with 3 rows and 3 columns
      int c[][] = new int[3][3];
	  System.out.println("The result after subtraction of the given matrices is:");
      // subtract and print matrix
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            c[i][j] = a[i][j] - b[i][j];
            System.out.print(c[i][j] + " ");
         }
         System.out.println();
      }
   }
}

Output of the above code is as ?

The result after subtraction of the given matrices is:
0 0 0 
0 0 0 
2 2 1 

Using Nested while Loop

This is another way of subtracting two matrices in Java. Here, we use the nested while loop in the place of nested for loop. The implementation uses the same logic as the previous example.

Example

Here, we use the nested while loop to subtract two matrix in Java.

public class SubtractMatrices {
   public static void main(String args[]) {
      int mtr1[][] = { {1, 7}, {9, 2} };
      int mtr2[][] = { {6, 3}, {5, 1} };
      int subtract[][] = new int[2][2];
      int matrix_size = 2;
      System.out.println("First matrix defined as::");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(mtr1[i][j] + "\t");
         }
         System.out.println();
      }
      
      System.out.println("Second matrix defined as::");
      for (int i = 0; i < matrix_size; i++) {
         for (int j = 0; j < matrix_size; j++) {
            System.out.print(mtr2[i][j] + "\t");
         }
         System.out.println();
      }
      
      int i = 0;
      while(i < matrix_size) {
         int j = 0;
         while(j < matrix_size) {
            subtract[i][j] = mtr1[i][j] - mtr2[i][j]; 
            j++;
         }
         i++;
      }
      
      System.out.println("Difference betwen given matrices is:: ");
      i = 0;
      while(i < matrix_size) {
         int j = 0;
         while(j < matrix_size) {
            System.out.print(subtract[i][j] + "\t");
            j++;
         }
         System.out.println();
         i++;
      }
   }
}

On running the above code, it will show the following output ?

First matrix defined as::
1	7	
9	2	
Second matrix defined as::
6	3	
5	1	
Difference betwen given matrices is:: 
-5	4	
4	1
Updated on: 2024-09-13T15:54:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements