I need to write a code (in Python) to multiply matrices of any order. I wrote the following code, but I keep getting the following error: "'int' object is not subscriptable". What is wrong with my code? can you help me fix the problem? I want to understand why my code doesn't work. I checked the math on a paper, and it seems to be fine. thx!
def MatrixMatrixMultiply(A,B):
row_A = 0 #number of rows of A
col_A = 0 #number of columns of A (i)
row_B = 0 # number of rows of B (j)
col_B = 0 #number of columns of B
for itm1 in A:
row_A += 1
for itm2 in A[0]:
col_A += 1
for itm3 in B:
row_B += 1
for itm4 in B[0]:
col_B += 1
ResultMatrix = ([0]*col_B)*row_A
if (col_A > row_B) or (col_A < row_B):
return "Error! Multiplication is not possible"
else:
for i in range(row_A):
for j in range(col_B):
for k in range(col_A):
ResultMatrix[i][j] += (A[i][k])*(B[k][j])
return ResultMatrix
A = [[1,2,3],[4,5,6]]
B = [[7,8],[9,10],[11,12]]
MatrixMatrixMultiply(A,B)
I wrote the following code, but it doesn't work. And I Don't know why.