Skip to main content

Two matrix subtraction

Matrix subtraction is an operation between two matrices of the same size (i.e., same number of rows and same number of columns) that results in a new matrix whose elements are the differences between the corresponding elements of the original matrices.

Let's consider two matrices A and B of size m x n, where A and B have the same number of rows m and the same number of columns n. The matrix subtraction of A and B can be represented mathematically as:

A - B = C

where C is a new matrix of size m x n, and its elements are obtained by subtracting the corresponding elements of A and B. Mathematically, the (i,j)-th element of C is given by:

C(i,j) = A(i,j) - B(i,j)

Here's an example of two matrix subtraction:


A = [ 2  4 ]
    [ 3  7 ]

B = [ 1  3 ]
    [ 4  2 ]

A - B = [ 2-1  4-3 ]
        [ 3-4  7-2 ]

      = [ 1  1 ]
        [ -1  5 ]

So the result of the matrix subtraction of A and B is the matrix C = [ 1 1 ; -1 5 ].

Here given code implementation process.

/*
  C Program 
+ Perform two matrix subtraction
*/
#include <stdio.h>

#define ROW 3
#define COL 3

//Display the element of given 2d matrix
void show_data(int matrix[][COL]) {
  
  printf("-----------------\n");
  
  for (int i = 0; i < ROW; ++i) {

    for (int j = 0; j < COL; ++j) {

      printf("%4d", matrix[i][j]);
    }

    printf("\n");
  }

  printf("\n");
}
void matrix_subtraction(int matrix1[][COL],int matrix2[][COL])
{

  //This matrix are store the result of subtraction 
  int result[ROW][COL];

  for (int i = 0; i < ROW; ++i) {

    for (int j = 0; j < COL; ++j) {

      //Subtract matrix A [i] row and [j] columns into
      // Matrix B [i] row and [j] columns
      result[i][j] = matrix1[i][j] - matrix2[i][j];
      
    }
  }
  //Display Matrix Elements
  printf("  Matrix A\n");
  //print element of matrix1
  show_data(matrix1);


  printf("\n  Matrix B\n");
  //print element of matrix1
  show_data(matrix2);


  printf(" (Matrix A) - \n (Matrix B)\n");
  //Display resultant matrix
  show_data(result);

}
int main() {
  //Define matrix 1
  int matrix1[ROW][COL] = {
    {
      1, 2, 3
    },

    {
      2, -5, 2
    },
    {
      -7, 4, 3
    }
  };

   //Define matrix 2
  int matrix2[ROW][COL] = {
    {
      3, 1, 4
    },
    {
      1, 3, 3
    },
    {
      2, 2, 2
    }
  };

  matrix_subtraction(matrix1,matrix2);
  
  return 0;
}

Output

  Matrix A
-----------------
   1   2   3
   2  -5   2
  -7   4   3


  Matrix B
-----------------
   3   1   4
   1   3   3
   2   2   2

 (Matrix A) -
 (Matrix B)
-----------------
  -2   1  -1
   1  -8  -1
  -9   2   1
/*
  C++ Program
  Perform two matrix subtraction
*/
#include<iostream>
#define ROW 3
#define COL 3
using namespace std;

class MyMatrix {
  public:

  //Display the element of given 2d matrix
  void show_data(int matrix[ROW][COL]) {
    int row = ROW;
    int col = COL;
    cout << "---------\n";
    //Assume Both same N x M  size Matrix
    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) {

        cout << " " << matrix[i][j];
      }
      cout << "\n";
    }
    cout << "\n";
  }
  void matrix_subtraction(int matrix1[ROW][COL], int matrix2[ROW][COL]) {
    //Assume both matrix are equal size
    //Assume  N x N Matrix size
    int row = ROW;
    int col = COL;
    int result[ROW][COL];
    for (int i = 0; i < row; ++i) {
      for (int j = 0; j < col; ++j) {
        //subtract matrix A [i] row and [j] columns into
        //Matrix B [i] row and [j] columns
        result[i][j] = matrix1[i][j] - matrix2[i][j];
      }
    }
    cout << " Matrix A\n";
    //print element of matrix1
    this->show_data(matrix1);
    cout << "\n Matrix B\n";
    //print element of matrix1
    this->show_data(matrix2);
    cout << " (Matrix A) - \n (Matrix B)\n";
    //Display resultant matrix
    this->show_data(result);
  }
};
int main() {
  MyMatrix obj ;
  int matrix1[ROW][COL] = {
    {
      1,
      2,
      3
    },
    {
      2,
      -5,
      2
    },
    {
      -7,
      4,
      3
    }
  };
  int matrix2[ROW][COL] = {
    {
      3,
      1,
      4
    },
    {
      1,
      3,
      3
    },
    {
      2,
      2,
      2
    }
  };
  obj.matrix_subtraction(matrix1, matrix2);
  return 0;
}

Output

 Matrix A
---------
 1 2 3
 2 -5 2
 -7 4 3


 Matrix B
---------
 3 1 4
 1 3 3
 2 2 2

 (Matrix A) -
 (Matrix B)
---------
 -2 1 -1
 1 -8 -1
 -9 2 1
/*
  Java Program
  Perform two matrix subtraction
*/
public class MyMatrix {
 
  //Display the element of given 2d matrix
  public void show_data(int [][]matrix) {
    
    System.out.print("-----------------\n");
    
    //Assume Both same N x M  size Matrix
    int row = matrix.length;

    int col = matrix[0].length;

    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) {

        System.out.print("  "+ matrix[i][j]);
      }

      System.out.print("\n");
    }

    System.out.print("\n");
  }
  public void matrix_subtraction(int [][]matrix1,int [][]matrix2)
  {
    //Assume both matrix are equal size
    //Assume  N x N Matrix size
    int row = matrix1.length;

    int col = matrix1[0].length;

    //This matrix are store the result of subtraction 
    int [][]result = new int[row][col];

    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) {

        //subtract matrix A [i] row and [j] columns into
        //Matrix B [i] row and [j] columns
        result[i][j] = matrix1[i][j] - matrix2[i][j];
        
      }
    }
    System.out.print("  Matrix A\n");
    //print element of matrix1
    show_data(matrix1);


    System.out.print("\n  Matrix B\n");
    //print element of matrix1
    show_data(matrix2);


    System.out.print(" (Matrix A) - \n (Matrix B)\n");
    //Display resultant matrix
    show_data(result);

  }

  public static void main(String[] args) {

    MyMatrix obj = new MyMatrix();
    //Define matrix 1
    int [][]matrix1 = {
      {
        1, 2, 3
      },

      {
        2, -5, 2
      },
      {
        -7, 4, 3
      }
    };

    //Define matrix 2
    int [][]matrix2 = {
      {
        3, 1, 4
      },
      {
        1, 3, 3
      },
      {
        2, 2, 2
      }
    };
    obj.matrix_subtraction(matrix1,matrix2);

  }
}

Output

  Matrix A
-----------------
  1  2  3
  2  -5  2
  -7  4  3


  Matrix B
-----------------
  3  1  4
  1  3  3
  2  2  2

 (Matrix A) - 
 (Matrix B)
-----------------
  -2  1  -1
  1  -8  -1
  -9  2  1
/*
  C# Program
  Perform two matrix subtraction
*/
using System;
public class MyMatrix {

	//Display the element of given 2d matrix
	public void show_data(int[,] matrix) {

		Console.Write("-----------------\n");

		//Assume Both same N x M  size Matrix
		int row = matrix.GetLength(0);

		int col = matrix.GetLength(1);

		for (int i = 0; i < row; ++i) {

			for (int j = 0; j < col; ++j) {

				Console.Write("  " + matrix[i,j]);
			}

			Console.Write("\n");
		}

		Console.Write("\n");
	}
	public void matrix_subtraction(int[,] matrix1, int[,] matrix2) {
		//Assume both matrix are equal size
		//Assume  N x N Matrix size
		int row = matrix1.GetLength(0);

		int col = matrix1.GetLength(1);

		//This matrix are store the result of subtraction 
		int[,] result = new int[row,col];

		for (int i = 0; i < row; ++i) {

			for (int j = 0; j < col; ++j) {

				//subtract matrix A [i] row and [j] columns into
				//Matrix B [i] row and [j] columns
				result[i,j] = matrix1[i,j] - matrix2[i,j];

			}
		}
		Console.Write("  Matrix A\n");
		//print element of matrix1
		show_data(matrix1);


		Console.Write("\n  Matrix B\n");
		//print element of matrix1
		show_data(matrix2);


		Console.Write(" (Matrix A) - \n (Matrix B)\n");
		//Display resultant matrix
		show_data(result);

	}

	public static void Main(String[] args) {

		MyMatrix obj = new MyMatrix();
		//Define matrix 1
		int[,] matrix1 = {
			{
				1,
				2,
				3
			},

			{
				2,
				-5,
				2
			},
			{
				-7,
				4,
				3
			}
		};

		//Define matrix 2
		int[,] matrix2 = {
			{
				3,
				1,
				4
			},
			{
				1,
				3,
				3
			},
			{
				2,
				2,
				2
			}
		};
		obj.matrix_subtraction(matrix1, matrix2);

	}
}

Output

  Matrix A
-----------------
  1  2  3
  2  -5  2
  -7  4  3


  Matrix B
-----------------
  3  1  4
  1  3  3
  2  2  2

 (Matrix A) -
 (Matrix B)
-----------------
  -2  1  -1
  1  -8  -1
  -9  2  1
<?php
/*
  Php Program
  Perform two matrix subtraction
*/
class MyMatrix {
	//Display the element of given 2d matrix

	public 	function show_data($matrix) {
		echo("-----------------\n");
		//Assume Both same N x M  size Matrix
		$row = count($matrix);
		$col = count($matrix[0]);
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
	public 	function matrix_subtraction($matrix1, $matrix2) {
		//Assume both matrix are equal size
		//Assume  N x N Matrix size
		$row = count($matrix1);
		$col = count($matrix1[0]);
		//This matrix are store the result of subtraction 
		$result = array_fill(0, $row,  array_fill(0, $col, 0));
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				//subtract matrix A [i] row and [j] columns into
				//Matrix B [i] row and [j] columns
				$result[$i][$j] = $matrix1[$i][$j] - $matrix2[$i][$j];
			}
		}
		echo(" Matrix A\n");
		//print element of matrix1
		$this->show_data($matrix1);
		echo("\n Matrix B\n");
		//print element of matrix1
		$this->show_data($matrix2);
		echo(" (Matrix A) - \n (Matrix B)\n");
		//Display resultant matrix
		$this->show_data($result);
	}
};

function main() {
	$obj = new MyMatrix();
	//Define matrix 1
	$matrix1 = array(array(1, 2, 3), array(2, -5, 2), array(-7, 4, 3));
	//Define matrix 2
	$matrix2 = array(array(3, 1, 4), array(1, 3, 3), array(2, 2, 2));
	$obj->matrix_subtraction($matrix1, $matrix2);
}
main();

Output

 Matrix A
-----------------
 1 2 3
 2 -5 2
 -7 4 3


 Matrix B
-----------------
 3 1 4
 1 3 3
 2 2 2

 (Matrix A) -
 (Matrix B)
-----------------
 -2 1 -1
 1 -8 -1
 -9 2 1
/*
 Node Js Program
 Perform two matrix subtraction
*/
class MyMatrix {
	//Display the element of given 2d matrix
	show_data(matrix) {
		process.stdout.write("-----------------\n");
		//Assume Both same N x M  size Matrix
		var row = matrix.length;
		var col = matrix[0].length;
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}
			process.stdout.write("\n");
		}
		process.stdout.write("\n");
	}
	matrix_subtraction(matrix1, matrix2) {
		//Assume both matrix are equal size
		//Assume  N x N Matrix size
		var row = matrix1.length;
		var col = matrix1[0].length;
		//This matrix are store the result of subtraction 
		var result = Array(row).fill(0).map(() => new Array(col).fill(0));;
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				//subtract matrix A [i] row and [j] columns into
				//Matrix B [i] row and [j] columns
				result[i][j] = matrix1[i][j] - matrix2[i][j];
			}
		}
		process.stdout.write(" Matrix A\n");
		//print element of matrix1
		this.show_data(matrix1);
		process.stdout.write("\n Matrix B\n");
		//print element of matrix1
		this.show_data(matrix2);
		process.stdout.write(" (Matrix A) - \n (Matrix B)\n");
		//Display resultant matrix
		this.show_data(result);
	}
}

function main(args) {
	var obj = new MyMatrix();
	//Define matrix 1
	var matrix1 = [
		[1, 2, 3],
		[2, -5, 2],
		[-7, 4, 3]
	];
	//Define matrix 2
	var matrix2 = [
		[3, 1, 4],
		[1, 3, 3],
		[2, 2, 2]
	];
	obj.matrix_subtraction(matrix1, matrix2);
}
main();

Output

 Matrix A
-----------------
 1 2 3
 2 -5 2
 -7 4 3


 Matrix B
-----------------
 3 1 4
 1 3 3
 2 2 2

 (Matrix A) -
 (Matrix B)
-----------------
 -2 1 -1
 1 -8 -1
 -9 2 1
# Python 3 Program
# Perform two matrix subtraction
class MyMatrix :
  # Display the element of given 2d matrix
  def show_data(self, matrix) :
    print("-----------------")
    row = len(matrix)
    col = len(matrix[0])
    i = 0
    while (i < row) :
      j = 0
      while (j < col) :
        print(" ", matrix[i][j],end="")
        j += 1
      
      print(end="\n")
      i += 1
    
    print("\n")
  
  def matrix_subtraction(self, matrix1, matrix2) :
    row = len(matrix1)
    col = len(matrix1[0])
    result = [
      [0] * col
      for _ in range(row)
    ]
    i = 0
    while (i < row) :
      j = 0
      while (j < col) :
        # Matrix B [i] row and [j] columns
        # subtract matrix A [i] row and [j] columns into
        # Matrix B [i] row and [j] columns
        result[i][j] = matrix1[i][j] - matrix2[i][j]
        j += 1
      
      i += 1
    
    print(" Matrix A")
    self.show_data(matrix1)
    print(" Matrix B")
    self.show_data(matrix2)
    print(" (Matrix A) - \n (Matrix B)")
    self.show_data(result)
  

def main() :
  obj = MyMatrix()
  matrix1 = [
    [1, 2, 3],
    [2, -5, 2],
    [-7, 4, 3]
  ]
  matrix2 = [
    [3, 1, 4],
    [1, 3, 3],
    [2, 2, 2]
  ]
  obj.matrix_subtraction(matrix1, matrix2)


if __name__ == "__main__":
  main()

Output

 Matrix A
-----------------
  1  2  3
  2  -5  2
  -7  4  3


 Matrix B
-----------------
  3  1  4
  1  3  3
  2  2  2


 (Matrix A) - 
 (Matrix B)
-----------------
  -2  1  -1
  1  -8  -1
  -9  2  1
# Ruby Program 
# Perform two matrix subtraction
class MyMatrix 
	# Display the element of given 2d matrix
	def show_data(matrix) 
		print("-----------------\n")
		row = matrix.length
		col = matrix[0].length
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
	def matrix_subtraction(matrix1, matrix2) 
		row = matrix1.length
		col = matrix1[0].length
		result = Array.new(row){Array.new(col, 0)}
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				# Matrix B [i] row and [j] columns
				# subtract matrix A [i] row and [j] columns into
				# Matrix B [i] row and [j] columns
				result[i][j] = matrix1[i][j] - matrix2[i][j]
				j += 1
			end
			i += 1
		end
		print(" Matrix A\n")
		self.show_data(matrix1)
		print("\n Matrix B\n")
		self.show_data(matrix2)
		print(" (Matrix A) - \n (Matrix B)\n")
		self.show_data(result)
	end
end
def main() 
	obj = MyMatrix.new()
	matrix1 = [
		[1, 2, 3],
		[2, -5, 2],
		[-7, 4, 3]
	]
	matrix2 = [
		[3, 1, 4],
		[1, 3, 3],
		[2, 2, 2]
	]
	obj.matrix_subtraction(matrix1, matrix2)
end
main()

Output

 Matrix A
-----------------
 1 2 3
 2 -5 2
 -7 4 3


 Matrix B
-----------------
 3 1 4
 1 3 3
 2 2 2

 (Matrix A) - 
 (Matrix B)
-----------------
 -2 1 -1
 1 -8 -1
 -9 2 1

/*
 Scala Program
  Perform two matrix subtraction
*/
class MyMatrix {
	//Display the element of given 2d matrix
	def show_data(matrix: Array[Array[Int]]): Unit = {
		print("-----------------\n");
		var row: Int = matrix.length;
		var col: Int = matrix(0).length;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
	def matrix_subtraction(matrix1: Array[Array[Int]], matrix2: Array[Array[Int]]): Unit = {
		var row: Int = matrix1.length;
		var col: Int = matrix1(0).length;
		var result: Array[Array[Int]] = Array.fill[Int](row, col)(0);
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				//Matrix B [i] row and [j] columns
				//subtract matrix A [i] row and [j] columns into
				//Matrix B [i] row and [j] columns
				result(i)(j) = matrix1(i)(j) - matrix2(i)(j);
				j += 1;
			}
			i += 1;
		}
		print(" Matrix A\n");
        this.show_data(matrix1);
        print("\n Matrix B\n");
        this.show_data(matrix2);
        print(" (Matrix A) - \n (Matrix B)\n");
        this.show_data(result);
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyMatrix = new MyMatrix();
		var matrix1: Array[Array[Int]] = Array(
			Array(1, 2, 3),
			Array(2, -5, 2),
			Array(-7, 4, 3));
		var matrix2: Array[Array[Int]] = Array(
			Array(3, 1, 4),
			Array(1, 3, 3),
			Array(2, 2, 2));
  		obj.matrix_subtraction(matrix1, matrix2);
	}
}

Output

 Matrix A
-----------------
 1 2 3
 2 -5 2
 -7 4 3


 Matrix B
-----------------
 3 1 4
 1 3 3
 2 2 2

 (Matrix A) -
 (Matrix B)
-----------------
 -2 1 -1
 1 -8 -1
 -9 2 1
/*
  Swift 4 Program
  Perform two matrix subtraction
*/
class MyMatrix {
	//Display the element of given 2d matrix
	func show_data(_ matrix: [[Int]]) {
		print("-----------------\n");
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" ", matrix[i][j],terminator:"");
				j += 1;
			}
			print(terminator:"\n");
			i += 1;
		}
		print(terminator:"\n");
	}
	func matrix_subtraction(_ matrix1: [[Int]], _ matrix2: [[Int]]) {
		let row: Int = matrix1.count;
		let col: Int = matrix1[0].count;
		var result: [[Int]] = Array(repeating: Array(repeating: 0, count: col), count: row);
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				//Matrix B [i] row and [j] columns
				//subtract matrix A [i] row and [j] columns into
				//Matrix B [i] row and [j] columns
				result[i][j] = matrix1[i][j] - matrix2[i][j];
				j += 1;
			}
			i += 1;
		}
		print(" Matrix A");
		self.show_data(matrix1);
		print("\n Matrix B");
		self.show_data(matrix2);
		print(" (Matrix A) - \n (Matrix B)");
		self.show_data(result);
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	let matrix1: [
		[Int]
	] = [
		[1, 2, 3],
		[2, -5, 2],
		[-7, 4, 3]
	];
	let matrix2: [
		[Int]
	] = [
		[3, 1, 4],
		[1, 3, 3],
		[2, 2, 2]
	];
	obj.matrix_subtraction(matrix1, matrix2);
}
main();

Output

 Matrix A
-----------------

  1  2  3
  2  -5  2
  -7  4  3


 Matrix B
-----------------

  3  1  4
  1  3  3
  2  2  2

 (Matrix A) -
 (Matrix B)
-----------------

  -2  1  -1
  1  -8  -1
  -9  2  1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment