Programmin-in-Python

Subtraction of 2 Matrices

Dec 21st, 2020 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def main() :
  2.     row = int(input("Enter the number of rows in both the matrices : "))
  3.     col = int(input("Enter the number of column in both the matrices : "))
  4.    
  5.     M1 , M2 , sum_mat = [],[],[]
  6.    
  7.     for i in range(2) :    
  8.         for j in range(row) :
  9.             temp = []
  10.             for k in range(col) :
  11.                 if i == 0 :
  12.                     elem = eval(input("Enter the Element of the First Matrix at the Position Row : {} , Column : {} =====> ".format(j+1,k+1)))
  13.                 else : elem = eval(input("Enter the Element of the Second Matrix at the Position Row : {} , Column : {} =====> ".format(j+1,k+1)))
  14.                 temp.append(elem)
  15.             if i == 0 : M1.append(temp)
  16.             else : M2.append(temp)
  17.     print('\nMatrix A : ')
  18.     for l in M1 : print(l)
  19.     print("\nMatrix B : ")
  20.     for m in M2 : print(m)
  21.    
  22.     temp = []
  23.     for i in range(row) :
  24.         for j in range(col) :
  25.             sum_elem = M1[i][j] - M2[i][j]
  26.             temp.append(sum_elem)
  27.         sum_mat.append(temp)
  28.         temp = []
  29.    
  30.     print("\nResult Of Subtraction : ")
  31.     for i in sum_mat : print(i)
  32.    
  33. main()
Add Comment
Please, Sign In to add comment