如何在Python中将两个矩阵相乘 [英] How to multiply two matrices together in Python
本文介绍了如何在Python中将两个矩阵相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python的新手,正在尝试仅使用Python将多个矩阵相乘.为了简单起见,我们将说它们的大小将始终相同.我尝试了很多不同的方法,但是还没有弄清楚.这是两个矩阵:
Brand new to Python and am trying to multiply to matrices together using only Python. To keep it simple, we'll say that they will always be the same size. I have tried so many different ways, but haven't figured it out. Here are the two matrices:
matrix_a = [[1、2、3],[1、2、3],[1、2、3] matrix_b = [[3,2,1],[3,2,1],[3,2,1]]
matrix_a = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] matrix_b = [[3, 2, 1], [3, 2, 1], [3, 2, 1]]
预先感谢
推荐答案
对于此任务,我建议使用 numpy ,但是这里有一些建议应该工作:
I would recommend numpy for this task however here is something that should work:
def multi(x,y):
d = []
i = 0
while i < len(x):
j = 0
e = []
while j < len(y[0]):
k = 0
r = 0
while k < len(x[0]):
r += x[i][k] * y[k][j]
k += 1
j += 1
e.append(r)
d.append(e)
i += 1
print(d)
这篇关于如何在Python中将两个矩阵相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文