I have to check if the given matrices can be multiplied, and if yes, return the product. I cannot use numpy to calculate the product.
Example used:
A = [[1,2],[3,4]]
B = [[1,2,3,4,5],[5,6,7,8,9]]
Expected output: A*B = [[11,14,17,20,23],[23,30,37, 44,51]]
Here's my code and output:
def matrix_mult(A,B):
countA = 0
countB = 0
result = [[0]*len(B[0])]*len(A)
for i in range(len(A)):
if A[i][1]:
countA += 1
for i in range(len(B)):
if B:
countB += 1
if countA == countB:
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(A)):
result[i][j] += A[i][k]*B[k][j]
return result
A = [[1,2],[3,4]]
B = [[1,2,3,4,5], [5,6,7,8,9]]
matrix_mult(A,B)
output:
[[34, 44, 54, 64, 74], [34, 44, 54, 64, 74]]
Is there something wrong with the code/logic?