I need to multiply any two matrices (that have correct dimensions allowing it) in Python3 without using any imports except for math. (So no numpy).
That said, here is what I came up with:
def dotMatrix(m1, m2):
m1rows = len(m1[0])
m1cols = len(m1)
m2rows = len(m1[0])
m2cols = len(m2)
newcolumn = [0]*m2cols
m = [newcolumn]*m2rows
sum = 0
for a in range(m1rows):
for b in range(m2rows):
for c in range(m2cols):
sum += m1[a][c]*m2[c][b]
m[a][b] = sum
sum = 0
return m
a = [[1,4],[2,5],[3,6]]
b = [[7,9,11],[8,10,12]]
c = dotMatrix(a,b)
print(c)
It is supposed to return [[58,139],[64,154]]
, but instead it returns [[54, 68], [54, 68]]
. Why is this?
I am working off of the example on mathisfun