In [1]:
r1 = int (input('Enter the number of rows of first matrix: '))c1 = int (input('Enter the number of columns of first matrix: '))r2 = int (input('Enter the number of rows of second matrix: '))c2 = int (input('Enter the number of columns of second matrix: '))if r1 != r2 and c1 != c2: print ('Substraction is not possible, because the dimensions are not same.')else: print ('Substaction is possible.') print ('Enter the elemenets of the first matrix: ') A = [] for i in range(0, r1): r = [] for j in range(0, c1): r.append(int(input())) A.append(r) print ('Enter the elements of the second matrix: ') B = [] for i in range(0, r2): r = [] for j in range(0, c2): r.append(int(input())) B.append(r) #Performing Substraction sub = [] for i in range(0, r1): row = [] for j in range(0, c1): row.append(A[i][j] - B[i][j]) sub.append(row) print ('First matrix is: ') for i in range(0, r1): for j in range(0, c1): print (A[i][j], end = " ") print() print ('Second matrix is: ') for i in range(0, r2): for j in range(0, c2): print (B[i][j], end = " ") print() print ('Matrix after performing substraction from A to B: ') for i in range(0, r1): for j in range(0, c1): print (sub[i][j], end = " ") print() Enter the number of rows of first matrix: 3 Enter the number of columns of first matrix: 3 Enter the number of rows of second matrix: 3 Enter the number of columns of second matrix: 3 Substaction is possible. Enter the elemenets of the first matrix: 20 30 40 30 40 50 40 50 60 Enter the elements of the second matrix: 2 3 4 3 4 5 4 5 6 First matrix is: 20 30 40 30 40 50 40 50 60 Second matrix is: 2 3 4 3 4 5 4 5 6 Matrix after performing substraction from A to B: 18 27 36 27 36 45 36 45 54
In [ ]:
No comments:
Post a Comment