132. Python Program to find Subtraction of two Matrices.
How does this program work?
- In this program we are going to learn about how to find Subtraction of two matrices using Python.
- If rows and columns of given two matrices are same then only subtraction will be possible. Otherwise It is not possible to subtract a 2 × 3 matrix with a 3 × 2 matrix.
- Subtraction can be performed by corresponding their elements then result will be displayed on matrix format.
Here is the code
a=[]
n=int(input("Enter the size of the matrix: "))
print("Enter the elements: ")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
a.append(row)
print(a)
print("Display an array In Matrix Form: ")
for i in range(n):
for j in range(n):
print(a[i][j], end=" ")
print()
b=[]
n= int(input ("Enter the size of the matrix: "))
print("Enter the element: ")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
b.append(row)
print(b)
print("Display Array In Matrix Form: ")
for i in range(n):
for j in range(n):
print(b[i][j], end=" ")
print()
result = [[0,0], [0,0]]
for i in range(n):
for j in range(len(a[0])):
result[i][j] = a[i][j] - b[i][j]
print("Resultant Matrix is: ")
for r in result:
print(r)