Sat Jul 31 2021

Matrix Multiplication

File Name: matrix-multiplication.py

#!/usr/bin/evn python

# 3x3 matrix
x = [[1,2,3],
        [4 ,5,6],
        [7 ,8,9]]

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

# Result is 3x4
mtMul = [[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)):
           mtMul[i][j] += x[i][k] * y[k][j]

for mM in mtMul:
   print(mM) 


# ***** Output *****
# [40, 34, 28, 22]
# [112, 97, 82, 67]
# [184, 160, 136, 112]

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.