使用np.array的Python中的矩阵和数组

问题描述 投票:-2回答:3

假设我有这样的计算:

The law I am trying to apply to calculate xd and yd

在Python中执行此操作的最佳方法是什么?我来自Matlab背景,Matlab中的矩阵很容易处理。我试过numpy但它给了我这个错误信息:

ValueError: setting an array element with a sequence. 

这是我的代码片段:

for i in Xh:
    for u in Yh:    
        E= (np.array([(C,D),(E,F)]) * np.array([(i),(u)]) ) + np.array([Cx,Cy])

请注意XhYh是已经计算过的列表,这就是我使用for循环的原因(我知道我可以使用列表推导更快)。

编辑:这是我从这个主题得到的一个很好的解决方案,并且完美无缺。对于任何面临同样问题的人来说,它可以提供帮助。

a = np.matrix([[1,2],[3,4]])
v1 = np.matrix([[0],[1]])
v2 = np.matrix([[1],[1]])

res = np.dot(a,v1)+v2 # python version <= 3.5
res = a@v1+v2 # python version > 3.5 with the new dot operator @
python arrays math matrix np
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.