Mid2 35th_Multiplication_of_Two_Matrices

 # Program to multiply two matrices using nested loops


# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x3 matrix
Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]
# result is 3x3
result = [[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:
[114, 160, 60] [74, 97, 73] [119, 157, 112]

Comments

Popular posts from this blog

JAVA Lab Programs

HANGMAN GAME

WD 22. StylingLinks&Buttons