Posted on by Kalkicode
Code Matrix

# Two matrix subtraction

Matrix subtraction is another fundamental operation in linear algebra, closely related to matrix addition. It involves subtracting corresponding elements of two matrices to generate a new matrix. This operation is again based on the principle that the matrices being subtracted must have the same dimensions.

## Problem Statement

Given two matrices A and B of size ROW x COL, the task is to perform matrix subtraction and obtain a new matrix C, where each element c[i][j] is the result of subtracting the corresponding elements of matrices A and B, i.e., c[i][j] = a[i][j] - b[i][j].

## Description

Matrix subtraction involves subtracting the corresponding elements of two matrices to produce a new matrix. Let's explore matrix subtraction with an example.

Consider two matrices: Matrix A:

`````` 1  2  3
2 -5  2
-7  4  3``````

Matrix B:

``````3  1  4
1  3  3
2  2  2``````

Matrix subtraction involves subtracting the corresponding elements of the matrices:

``````(1-3)  (2-1)  (3-4)
(2-1)  (-5-3) (2-3)
(-7-2) (4-2)  (3-2)``````

This results in the following matrix:

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

## Idea to Solve the Problem

The approach to solving the problem of matrix subtraction is quite similar to that of matrix addition:

1. Define two matrices A and B with the same dimensions (ROW x COL).
2. Iterate through each element of the matrices and calculate the difference of the corresponding elements.
3. Store the differences in a new matrix C of the same dimensions.
4. Display the matrices A, B, and C to showcase the inputs and the result of the subtraction.

## Pseudocode

Here's the pseudocode to perform matrix subtraction:

``````function matrixSubtraction(matrix1, matrix2):
initialize result matrix of the same dimensions as matrix1 and matrix2

for i from 0 to ROW - 1:
for j from 0 to COL - 1:
result[i][j] = matrix1[i][j] - matrix2[i][j]

display "Matrix A:"
display matrix1
display "Matrix B:"
display matrix2
display "Matrix A - Matrix B:"
display result``````

## Algorithm Explanation

1. The function `matrixSubtraction` takes two matrices `matrix1` and `matrix2` as input and initializes a new matrix `result` of the same dimensions.
2. It then iterates through each element using two nested loops, where `i` represents the row index and `j` represents the column index.
3. For each element, it calculates the difference of the corresponding elements from `matrix1` and `matrix2` and stores it in the corresponding position of the `result` matrix.
4. After calculating the entire `result` matrix, the function displays the original matrices `matrix1` and `matrix2`, and then displays the result matrix `result`.

## Code Solution

``````/*
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
``````

## Time Complexity

The time complexity of this code is the same as that of the matrix addition code. The nested loops iterate through all the elements of the matrices, resulting in a time complexity of O(ROW * COL), where ROW and COL are the number of rows and columns in the matrices, respectively.

## 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.

Categories
Relative Post