codescracker


python

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[0])):
		rmatrix[i][j] = matrix1[i][j] - matrix2[i][j]

		
for r in rmatrix:
	print(r)

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

python subtract two matrices

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test