0

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.

  • Hey, thanks for your comment. I can't use the "len" function, but I sure know what it does. Regarding the () parentheses - my mistake. I fixed it, but something is still not right. – Guy Weizman Apr 23 '23 at 21:13
  • 1
    Does that mean you're not allowed to use the `len` function? Most certainly you're not supposed to re-implement it then, either. – Kelly Bundy Apr 23 '23 at 21:16
  • You are right. I'm not allowed to use it. I'll be more accurate - I'm not sure whether I'm allowed to use it or not, so I don't want to take any risks of losing points. – Guy Weizman Apr 23 '23 at 21:17
  • 1
    They deduct points without telling you whether it's allowed? Odd. Anyway, I wouldn't even *want* to use it. Nicer [without](https://ato.pxeger.com/run?1=dY9NDoIwEIX3PcUsKRkTi39o4qLsPUHTBSrEJlhILVG8ihs2eidv4DFsEYwbZzE_771ZfLdH1dhDqdv2Xtt8FD9f-yyHTWqNuvS9LqyqiibgmNAVAVcms7XRILrDlzjVxyCFELaQlwZSdIvScFVVYMozR9iVRULpN9-lvDakwoRK8mv6N2_yTpWEcFiDEAwjnEgUU5zhXEqSdOoCY6ctkY3dYAxZ5KzKKG2DvyT0w9tjD_hv) :-) – Kelly Bundy Apr 23 '23 at 21:32

2 Answers2

1

You have to create each row individually to avoid duplicate references as @user2357112 in his link:

# Fixed by @slothrop
ResultMatrix = [[0]*col_B for _ in range(row_A)]

Output:

>>> MatrixMatrixMultiply(A,B)
[[58, 64], [139, 154]]

Check with numpy:

A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8],[9,10],[11,12]])

>>> A @ B
array([[ 58,  64],
       [139, 154]])
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • 1
    It's more a matter of creating each *row* (rather than cell) individually. `[[0]*col_B for _ in range(row_A)]` would be fine. (As explained in detail in the top answer to the question you linked: https://stackoverflow.com/a/240205/765091) – slothrop Apr 23 '23 at 21:13
  • @slothrop. Thanks, you are right. I fixed my answer according your comment and gives you the credit. – Corralien Apr 23 '23 at 21:17
0

Problem solved. Thanks to anyone who helped me understand what was wrong with my code.

The main problem was the way I created the product matrix. I duplicated a specific cell when I should have created the matrix using list comprehension. After I had fixed that, the function worked fine.

Thanks again!