import numpy as np
x = np.random.random((3,3)) #point-set 1
y = np.random.random((3,3)) #point-set 2
neighbors_of_x = np.array([[0,1],[0],[0]])
#^^ contains indices of neighbors of points in x, in y.
#each point can have different number of neighbors in y.
#Below, for every point in x, subtract from its neighbor in y
for i in range(x.shape[0]):
print(x[i,:] - y[neighbors_of_x[i],:])
以上是代表性示例;实际的点集可能有1000个点。因此,我想“向量化”最后两行(for循环)
有没有一种方法可以最终不进行for循环,而每个索引i
都是并行处理的?
注意:由于neighbors_of_x
在每个i
处可以具有不同数量的索引,因此该数组的类型为object
,因此我无法直接使用它来选择{ {1}}