Python Program to Multiply Two Matrices – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Multiply Two Matrices.

Code

# Python Program to Multiply Two Matrices

# 3x3 matrix
X = [[10,8,2],
    [2 ,1,4],
    [4 ,4,6]]

# 3x4 matrix
Y = [[1,10,9,8],
    [2,11,12,7],
    [3,4,5,6]]

# result is 3x4
result = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

Output

[32, 196, 196, 148]
[16, 47, 50, 47]
[30, 108, 114, 96]

Check out our other python programming examples

Leave a Comment

Your email address will not be published. Required fields are marked *