# Python Program to Subtract Two Matrices

« Previous Program Next Program »

## Subtract Two Matrices in Python

To subtract two matrices in python, first choose three matrices. First matrix, second matrix, and third matrix (to store the subtraction result to the third/this matrix). Initially, all the content of the third matrix will be 0. In other word, initially third matrix is an empty matrix.

## Python Programming Code to Subtract Two Matrices

Following python program shows the subtraction of one matrix from another matrix in python:

```# Python Program - Subtract Two Matrices

matrix1 = [[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]
matrix2 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
rmatrix = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix1)):
rmatrix[i][j] = matrix1[i][j] - matrix2[i][j]
for r in rmatrix:
print(r)
```

Subtraction of the two given matrix in above program can be calculated as:

• matrix1 - matrix2 = 10 - 1 = 9 = rmatrix
• matrix1 - matrix2 = 11 - 2 = 9 = rmatrix
• matrix1 - matrix2 = 12 - 3 = 9 = rmatrix
• matrix1 - matrix2 = 13 - 4 = 9 = rmatrix
• matrix1 - matrix2 = 14 - 5 = 9 = rmatrix
• matrix1 - matrix2 = 15 - 6 = 9 = rmatrix
• matrix1 - matrix2 = 16 - 7 = 9 = rmatrix
• matrix1 - matrix2 = 17 - 8 = 9 = rmatrix
• matrix1 - matrix2 = 18 - 9 = 9 = rmatrix

Here is the sample run of the above python program to illustrate how to subtract one matrix from another matrix:

Here is the same program on python shell:

#### Same Program in Other Languages

Python Online Test

« Previous Program Next Program »