Python Programming

Matrix Multiplication

Python programming for matrix multiplication using 2D array and for loop iteration

7/31/2021
0 views
matrix-multiplication.pyPython
#!/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]
Pythonpython programmingcodingmatrix multiplication2D arrayfor loopiteration

Related Examples

Mashable is a global, multi-platform media and entertainment company For more queries and news contact us on this
Email: info@mashablepartners.com