# Program to Subtract two Matrices in Python & C++ Programming

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called as 5*4 matrix.

What is matrix subtraction:

Given two matrices of same order(dimensions) then we can easily add the two matrices by doing the difference of corresponding elements in both the matrices.

Example:

Here Matrix C is matrix subtraction  of the matrices A and B.

Input:

Matrix 1  =    2    3   1
1    2   3
-3   0   2
Matrix 2 =      1    4    2
-4    5   -1
2    1     4

Output:

Printing the difference of matrices :
1 -1 -1
5 -3 4
-5 -1 -2

Explanation:

 Here we subtracted the corresponding elements in both matrices

## Program for Matrix Subtraction

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### Method #1:Using nested Loops in Python

A matrix can be implemented as a nested list in Python (list inside a list). Each variable can be thought of as a row in the matrix.

Approach:

• Create two two-dimensional arrays, a and b, and set their values. (In this case, we used a  initialised statically)
• Calculate the number of rows and columns in the array a (since the dimensions of both arrays are the same) and store it in the variables rows and columns.
Declare a new array diff, this time with dimensions in rows and columns.
• Calculate the difference between the respective elements by looping around the arrays a and b.
• Print the difference of two matrices.

Below is the  implementation:

# given matrix A
A = [[2, 3, 1],
[1, 2, 3],
[-3, 0, 2]]
# given matrix B
B = [[1, 4, 2],
[-4, 5, -1],
[2, 1, 4]]
# Initialize the difference of matrices elements to 0
matrixDiff = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# Traverse the rows
for rows in range(len(A)):
# Traverse the  columns
for columns in range(len(A[0])):
matrixDiff[rows][columns] = A[rows][columns] - B[rows][columns]
# printing the difference of matrices
print("Printing the difference of matrices : ")
for rows in matrixDiff:
print(*rows)


Output:

Printing the difference of matrices :
1 -1 -1
5 -3 4
-5 -1 -2

### Method #2:Using List Comprehension in Python

The program’s output is the same as previously. For iterating through each element in the array, we used the nested list comprehension.

Understanding the list helps us to write concise codes and we need to always try to use them in Python. They’re really beneficial.

Below is the implementation:

# given matrix A
A = [[2, 3, 1],
[1, 2, 3],
[-3, 0, 2]]
# given matrix B
B = [[1, 4, 2],
[-4, 5, -1],
[2, 1, 4]]
# Initialize the difference of matrices elements to 0
matrixDiff = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

# using list comprehension
matrixDiff = [[A[i][j] - B[i][j]
for j in range(len(A[0]))] for i in range(len(A))]

# printing the difference of matrices
print("Printing the difference of matrices : ")
for rows in matrixDiff:
print(*rows)


Output:

Printing the sum of matrices :
16 -1 -5
6 11 6

### Method #3:Using nested loops in C++

We used nesting loops in this program to iterate through and row and column. In the two matrices we will subract the appropriate elements and store them in the result at each level.

Let us take dynamic input in this case.

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{
int rows, columns, A[100][100], B[100][100], DiffMatrix[100][100], i, j;
cout << "Enter the number of rows of the matrix "<<endl;
cin >> rows;
cout << "Enter the number of columns of the matrix"<<endl;
cin >> columns;
cout << "Enter the elements of first matrix " << endl;
// Initializing matrix A with the user defined values
for(i = 0; i < rows; ++i)
for(j = 0; j < columns; ++j)
{
cout << "Enter element A" << i + 1 << j + 1 << " = ";
cin >> A[i][j];
}
// Initializing matrix B with the user defined values
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < rows; ++i)
for(j = 0; j < columns; ++j)
{
cout << "Enter element B" << i + 1 << j + 1 << " = ";
cin >> B[i][j];
}
// Performing matrix addition by doing diffeerence of given two matrices A and B
for(i = 0; i < rows; ++i)
for(j = 0; j < columns; ++j)
DiffMatrix[i][j] = A[i][j] - B[i][j];
//printing matrix A
cout << endl << " printing the matrix A" << endl;
for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
cout << A[i][j] << "  ";
}
cout << endl;
}
//printing matrix B
cout << endl << " printing the matrix B" << endl;
for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
cout << B[i][j] << "  ";
}
cout << endl;
}
//printing the difference of matrices
cout << endl << " printing the difference of matrices A and B" << endl;

for (i = 0; i < rows; ++i) {
for (j = 0; j < columns; ++j) {
cout << DiffMatrix[i][j] << "  ";
}
cout << endl;
}
return 0;
}

Output:

Enter the number of rows of the matrix
4
Enter the number of columns of the matrix
4
Enter the elements of first matrix
Enter element A11 = 1
Enter element A12 = 2
Enter element A13 = 3
Enter element A14 = 4
Enter element A21 = 5
Enter element A22 = 6
Enter element A23 = 5
Enter element A24 = 2
Enter element A31 = -5
Enter element A32 = 9
Enter element A33 = 1
Enter element A34 = 5
Enter element A41 = 0
Enter element A42 = 5
Enter element A43 = 2
Enter element A44 = 5

Enter elements of 2nd matrix:
Enter element B11 = 1
Enter element B12 = 9
Enter element B13 = 5
Enter element B14 = 3
Enter element B21 = 8
Enter element B22 = -4
Enter element B23 = -8
Enter element B24 = 0
Enter element B31 = 5
Enter element B32 = 1
Enter element B33 = 7
Enter element B34 = -3
Enter element B41 = -8
Enter element B42 = 1
Enter element B43 = 2
Enter element B44 = 0

printing the matrix A
1 2 3 4
5 6 5 2
-5 9 1 54
0 5 2 5

printing the matrix B
1 9 5 3
8 -4 -38 0
5 1 7 -3
-8 1 2 0

printing the difference of matrices A and B
0 -7 -2 1
-3 10 43 2
-10 8 -6 57
8 4 0 5

Related Programs: