Untitled
unknown
plain_text
10 months ago
669 B
16
Indexable
def matrixMultiply(a, b):
if len(a[0]) != len(b):
return []
result = []
for i_idx in range(len(a)):
product = []
for k_idx in range(len(a)):
total = 0
for j_idx in range(len(a[i_idx])):
total += a[i_idx][j_idx] * b[j_idx][k_idx]
product.append(total)
result.append(product)
return result
a = [
[1, 2],
[3, 4]
]
b = [
[5, 6],
[7, 8]
]
print(matrixMultiply(a,b))
# [
# [19,22],
# [43,50]
# ]
c = [
[1,2,3],
[4,5,6]
]
d = [
[1,2],
[3,4],
[5,6]
]
print(matrixMultiply(c,d))
# [
# [22, 28],
# [49, 64]
# ]Editor is loading...
Leave a Comment