Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- # https://www.reddit.com/r/learnpython/comments/5ai9vf/my_code_only_takes_square_matrixes_why/
- #
- print("\nEnter the value of row and column of first matrix :")
- r1 = int(input("Rows: "))
- c1 = int(input("Columns: "))
- # to make a blank X matrix:
- X = [[0 for column in range(c1)] for row in range(r1)]
- print("\nEnter elements of first matrix : ")
- for row in range(r1):
- for column in range(c1):
- X[row][column] = int(input("[{},{}]: ".format(row, column)))
- #----------------------------------------------------------------
- print("\nEnter the value of row and column of second matrix : ")
- r2 = int(input("Rows: "))
- c2 = int(input("Columns: "))
- # to make blank Y matrix:
- Y =[[0 for column in range(c2)] for row in range(r2)]
- print("\nEnter elements of second matrix : ")
- for row in range(r2):
- for column in range(c2):
- Y[row][column] = int(input("[{},{}]: ".format(row, column)))
- #----------------------------------------------------------------
- print("\nElements of First matrix is : ")
- for row in X:
- print(row)
- print("\nElements of Second matrix is :")
- for row in Y:
- print(row)
- #----------------------------------------------------------------
- # result takes the result raw and column to make a blank result matrix
- result = [[0 for column in range(c1)] for row in range(r2)]
- for row in range(len(X)):
- for column in range(len(Y[0])):
- for k in range(len(Y)):
- result[row][column] += X[row][k] * Y[k][column]
- print("\nMATRIX MULTIPLICATION:")
- for row in result:
- print(row)
Add Comment
Please, Sign In to add comment