我有一个带有点的3d网格,点的位置是 在一个如下所示的数组中:
Dictionary<String,Int>
我有两个索引数组:
mesh_vectors = np.array([[-0.85758871, 0.8965745 , -0.1427767 ],
[-0.23945311, 1.00544977, 1.45797086],
[-0.57341832, -1.07448494, -0.11827722],
[ 0.05894491, -0.97208506, 1.47583127],
[-0.71402085, -0.08872638, -0.12916484],
[-0.09181146, 1.01235461, 0.47418442],
[-0.09025362, 0.01668115, 1.46690106],
[ 0.19773833, -0.95349348, 0.49089319],
[ 0.05055711, 0.02909645, 0.48503664]])
这些翻译对应于索引数组:
idx1 = np.array([4 2 1 6 5 0 1 5])
idx2 = np.array([6 3 0 4 7 2 3 7])
它们目前被添加到网格中:
translate_1 = np.array([[ 0.00323021 0.00047712 -0.00422925]
[ 0.00153422 0.00022654 -0.00203258]
[ 0.00273207 0.00039626 0.00038201]
[ 0.0052439 0.00075993 0.00068843]
[-0.00414245 -0.00053918 0.00543974]
[-0.00681844 -0.00084955 0.00894626]
[ 0. 0. 0. ]
[-0.00672519 -0.00099897 -0.00090189]])
translate_2 = np.array([[ 0.00523871 0.00079512 0.00068814]
[ 0.00251901 0.00038234 0.00033379]
[ 0.00169134 0.00021078 -0.00218737]
[ 0.00324106 0.00040338 -0.00422859]
[-0.00413547 -0.00058669 0.00544016]
[-0.00681223 -0.0008921 0.00894669]
[ 0. 0. 0. ]
[-0.00672553 -0.00099677 -0.00090191]])
麻烦的是,我真正需要添加的不是翻译 但是翻译的平均值是多个翻译 应用于同一网格点。索引数组可以具有以各种不同频率出现的索引。可能是[2,2,2,3,4,5]和[1,2,1,1,5,4]虽然它们总是大小相同。我正在尝试使用numpy来提高速度,但我可以选择在start时使用循环来生成索引数组(如果需要)。 提前谢谢!
答案 0 :(得分:0)
这有效:
scaled_tr1 = translate_1 / np.bincount(idx1)[idx1,None]
np.add.at(mesh_vectors, idx1, scaled_tr1)
请注意,使用np.add.at
代替花式索引is required:
ufunc.at(a,indices,b = None)
对
a
指定的元素在操作数indices
上执行无缓冲的就地操作。对于add ufunc,此方法等效于a[indices] += b
,除了为多次索引的元素累积结果。例如,a[[0,0]] += 1
只会因缓冲而增加第一个元素,而add.at(a, [0,0], 1)
会增加第一个元素两次。