List - Matrix Multiplication

# MatrixMultiplication

Mat_A = []
Mat_B = []
Mat_Mul = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Multiplication
if (c1 != r2):
    print("Rows and columns do not match.")
    print("Cannot multiply.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
       
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c2,1):
            ele = 0
            for k in range(0,c1,1):
                ele = ele + (Mat_A[i][k] * Mat_B[k][j])
            tmp.append(ele)
        Mat_Mul.append(tmp)
       
    print("Product of given matrices : ")
   
    for row in Mat_Mul:
        for ele in row:
            print(ele, end=" ")
        print()

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu