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.
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:
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:
© Copyright 2021. All Rights Reserved.