Subtraction of Two Matrix in C

In this post we will see the subtraction of two matrix in C. We will also develop the C program to subtract two matrix using functions.

Let A =[aij] and B =[bij] be m × n matrices. The subtract of A and B, denoted by A – B, is the m × n matrix that has aij – bij as its (i, j)th element. In other words, A – B =[aij – bij].

The subtraction of two matrices of the same size is obtained by subtracting elements in the corresponding positions. 

Condition for matrix subtraction:- Both matrices should have same size.

Matrices of different sizes cannot be subtracted, because the subtraction of two matrices is defined only when both matrices have the same number of rows and the same number of columns.

     a11  a12
A = 
     a21  a22
     b11  b12
B = 
     b21  b22

The subtraction of A and B can be calculated as,

        a11-b11  a12-b12
A - B = 
        a21-b21  a22-b22

Simple Program

This is the simple C Program without using any function. The values for the matrices are already given in the program itself.

#include <stdio.h>
int main()
{
  // matrix A and B
  int a[2][2] = {{5,6},{9,8}};
  int b[2][2] = {{1,3},{3,2}};

  // declare matix C
  int c[2][2];

  // subtract both matrix A and B
  for(int i=0; i<2; i++)
  {
    for(int j=0; j<2; j++)
    {
      // subtract & store to matrix C
      c[i][j] = a[i][j] - b[i][j];
    }
  }

  // display matrix
  printf("Resultant Matrix: \n");
  for(int i=0; i<2; i++)
  {
    for(int j=0; j<2; j++)
    {
      printf("%d ", c[i][j]);
    }
    printf("\n"); // new line
  }

  return 0;
}

Output:-

Resultant Matrix:
4 3
6 6

In this program, we had taken two 2×2 matrix. Since both matrices are of same size therefore matrix subtraction is possible, and new resultant matrix also will be the same size. 

Algorithm for Matrix subtraction in C Program

a) Take three matrices (matrix1 & metrix2 & matrix3)
b) Use nested loop to iterate the matrix1 and matrix2
c) Find the subtraction of crosspounding elements in matrix1 & matrix2 and assign them to matrix3

Function

// function to subtract two matrix
void subtractMatrix(int a[10][10], int b[10][10], 
                    int c[10][10], int row, int column)
{
  for(int i=0; i< row; ++i)
  {
    for(int j=0; j< column; ++j)
    {
      // subtract & store to matrix C
      c[i][j] = a[i][j] - b[i][j];
    }
  }
}

The subtractMatrix() function takes 5 parameters, first three parameters for matrix a, b, c and remaining two parameters for row and column. It’s return type is void, and it returns nothing. The arrays are implicit pointers and therefore internally values are stored to the original variables.

Time complexity:- O(N2)

If both matrix have same row and column size then move to the next step else matrix subtractionis not possible, return back.

Subtraction of 2 Matrices using Function

In this C program, We will use the user-defined functions in these programs, so the function in C knowledge should be there. Along with this, you must know how to pass multidimensional array to a function.

C Program for Matrix subtraction using Functions

#include <stdio.h>
// function to subtract two matrix
void subtractMatrix(int a[10][10], int b[10][10], 
                    int c[10][10], int row, int column)
{
  for(int i=0; i< row; ++i)
  {
    for(int j=0; j< column; ++j)
    {
      // subtract & store to matrix C
      c[i][j] = a[i][j] - b[i][j];
    }
  }
}
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      scanf("%d", &matrix[i][j]);
    }
  }
}
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      printf("%d ", matrix[i][j]);
    }
    printf("\n"); // new line
  }
}
// main function
int main()
{
  // declare matrix matrix A, B, & C
  int a[10][10]; // first matrix
  int b[10][10]; // second matrix
  int c[10][10]; // resultant matrix

  // read the size of matrices
  int row, column;
  printf("Enter Row and Column Sizes: ");
  scanf("%d %d", &row, &column);

  // read matrix A and B
  printf("Enter Matrix-1 Elements: \n");
  readMatrix(a, row, column);
  printf("Enter Matrix-2 Elements: \n");
  readMatrix(b, row, column);

  // subtract both matrix A and B
  subtractMatrix(a, b, c, row, column);

  // display resultant matrix
  printf("Resultant Matrix: \n");
  displayMatrix(c, row, column);

  return 0;
}

Output for different input values,

Enter Row and Column Sizes: 2 2
Enter Matrix-1 Elements:
1 3
7 5
Enter Matrix-2 Elements:
6 8
4 2
Resultant Matrix:
-5 -5
3 3

Enter Row and Column Sizes: 3 3
Enter Matrix-1 Elements:
4 6 8
7 5 3
9 5 1
Enter Matrix-2 Elements:
1 2 3
4 5 6
7 8 9
Resultant Matrix:
3 4 5
3 0 -3
2 -3 -8

If we are asking row and column size seperatly then before finding the subtraction of two matrix, first we should check both are having same size or not i.e. the row and column of both matrix must be equal else matrix subtraction is not possible. Find the row and column size of both matrix, if they are same then declare a new matrix with the same row and column size, which will store the resultant values. Then proced further to find the subtraction of matrices.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *