Skip to content

Instantly share code, notes, and snippets.

@lunaroyster
Created August 25, 2017 17:26
Show Gist options
  • Save lunaroyster/3938e62d853c627e7a0c5ff74a46d4ef to your computer and use it in GitHub Desktop.
Save lunaroyster/3938e62d853c627e7a0c5ff74a46d4ef to your computer and use it in GitHub Desktop.
def multiply(m1, m2):
x1 = len(m1[0])
y1 = len(m1)
x2 = len(m2[0])
y2 = len(m2)
if(x1!=y2):
return("Error. Bad Matrix")
x3 = x2
y3 = y1
c = x1
Result = [[0 for x in range(x3)] for y in range(y3)]
for x in range(x3):
for y in range(y3):
answer = 0
for i in range(c):
product = m1[x][i] * m2[i][y]
answer+=product
Result[x][y] = answer
return(Result)
Matrix1 = [[1, 2], [3, 4]]
Matrix2 = [[2, 0], [1, 2]]
print(multiply(Matrix1, Matrix2))
Matrix1 = [[1, 2, 3], [4, 5, 6]]
Matrix2 = [[7, 8], [9, 10], [11, 12]]
print(multiply(Matrix1, Matrix2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment