添加新行会导致中的ValueErrornumpy.asarray公司

2024-04-24 17:05:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在添加neighbor语句(我用'new'注释)之前,一切正常。现在,当使用numpy.asarray公司,出现以下错误:

ValueError:无法将输入数组从形状(3,3)广播到形状(3)。

我真的很困惑,因为新行没有改变任何关于旋转数组。你知道吗

def rre(mesh, rotations):
"""
Relative Rotation Encoding (RRE).
Return a compact representation of all relative face rotations.
"""
all_rel_rotations = neighbors = []
for f in mesh.faces():
    temp = [] # new
    for n in mesh.ff(f):
        rel_rotation = np.matmul(rotations[f.idx()], np.linalg.inv(rotations[n.idx()]))
        all_rel_rotations.append(rel_rotation)
        temp.append(n.idx()) # new
    neighbors.append(temp) # new
all_rel_rotations = np.asarray(all_rel_rotations)
neighbors = np.asarray(neighbors) # new
return all_rel_rotations, neighbors

Tags: innewfornpneighbors数组alltemp
1条回答
网友
1楼 · 发布于 2024-04-24 17:05:02

问题的根源很可能是:

all_rel_rotations = neighbors = []

在Python中,列表是可变的,并且all_rel_rotationsneighbors指向同一个列表,因此如果执行all_rel_rotations.append(42),您将看到neighbors = [42, ]

线路:

all_rel_rotations.append(rel_rotation)

附加二维数组,而

neighbors.append(temp)

将1D数组(或相反)附加到同一列表。然后:

all_rel_rotations = np.asarray(all_rel_rotations)

尝试转换为数组并感到困惑。你知道吗

如果你需要做什么

all_rel_rotations = []
neighbors = []

相关问题 更多 >