In: Computer Science
PYTHON CODE:
def square_matrix_multiplication(matrix1,matrix2):
C=[[0 for i in range(len(matrix1))] for i in
range(len(matrix2))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
C[i][j]=0
for k in range(len(matrix2)):
C[i][j] += matrix1[i][k]*matrix2[k][j]
return C
I use that function in my code. When I type like "return C", It does not work. If I type print(C), then my code works. Why is it happening?
I created two user-entered matrices above the function, then I called the function.
def square_matrix_multiplication(matrix1,matrix2):
C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
C[i][j]=0
for k in range(len(matrix2)):
C[i][j] += matrix1[i][k]*matrix2[k][j]
return C
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
Z = square_matrix_multiplication(X,Y)
print(Z)
Your code is working fine. I have attached the image for your help.