def matrixmulti(mat1,mat2):
result=[]
'''
for i in range(len(mat1)):
for j in range(len(mat2[0])):
for k in range(len(mat2)):
result[i][j] += mat1[i][k] * mat2[k][j]
print (result[i][j])
'''
result = [[sum(a * b for a, b in zip(mat1_row, mat2_col))
for mat2_col in zip(*mat2)]
for mat1_row in mat1]
for i in range(len(mat1)):
for j in range(len(mat2[0])):
print(mat[i][j],end=" ")
print()
我首先尝试使用嵌套for循环,由于某种原因它没有执行,但是嵌套列表也发生了同样的事情。 有人能帮我找出哪里做错了什么吗? 我刚刚传递了两个矩阵的函数,这两个矩阵是全局初始化的。你知道吗
你在代码中犯的错误很少,你可以通过修正来让它正常工作。 我已经制作了一个固定的三重循环版本,这里有一些注释来说明如何正确地执行它:
你犯了几个错误:
matrixmulti
函数的结果,或者在函数内部打印结果,这样它才有任何帮助希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐