我是 python 新手,我正在编写一个矩阵程序,但是有一个问题我不知道如何获得正确的输出,我需要帮助。这就是问题:给定一个 nXn 矩阵 A 和一个 kXn 矩阵 B 找到 AB 。这就是我到目前为止所拥有的。先感谢您
def matrixmult (A, B):
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
if cols_A != rows_B:
print "Cannot multiply the two matrices. Incorrect dimensions."
return
# Create the result matrix
# Dimensions would be rows_A x cols_B
C = [[0 for row in range(cols_B)] for col in range(rows_A)]
print C
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
C[i][j] += A[i][k]*B[k][j]
return C