Write a Python Program to multiply matrices. 

Program Algorithm:
1. Define two matrices X and Y
2. Create a resultant matrix named ‘result’
3. for i in range(len(X)):
 i. for j in range(len(Y[0])):
a) for k in range(len(Y))
b) result[i][j] += X[i][k] * Y[k][j]
4. for r in result, print the value of r  

Program Code:
X = [[12,7,3],
 [4 ,5,6],
 [7 ,8,9]]
Y = [[5,8,1,2],
 [6,7,3,0],
 [4,5,9,1]]
result = [[0,0,0,0],
 [0,0,0,0],
 [0,0,0,0]]
for i in range(len(X)):
 for j in range(len(Y[0])):
 for k in range(len(Y)):
 result[i][j] += X[i][k] * Y[k][j]
for r in result:
 print(r)   



Program Output:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23] 
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: