Python 具有多索引数组的Numpy混合数组
我有一个带有点的3d网格,点的位置是 在如下所示的数组中:Python 具有多索引数组的Numpy混合数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个带有点的3d网格,点的位置是 在如下所示的数组中: mesh_vectors = np.array([[-0.85758871, 0.8965745 , -0.1427767 ], [-0.23945311, 1.00544977, 1.45797086], [-0.57341832, -1.07448494, -0.11827722],
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]])
mesh_vectors[idx1] += translate_1
mesh_vectors[idx2] += translate_2
我有两个索引数组:
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]])
它们当前添加到网格中,如下所示:
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]])
mesh_vectors[idx1] += translate_1
mesh_vectors[idx2] += translate_2
问题是,我真正需要补充的不是翻译
但是翻译的平均值是多个翻译
应用于同一网格点。索引数组可以具有以各种不同频率出现的索引。可以是[2,2,2,3,4,5]和[1,2,1,1,5,4],尽管它们的大小始终相同。为了提高速度,我尝试使用numpy来实现这一点,但如果需要,我可以选择在开始时使用循环来生成索引数组。
提前谢谢 这是有效的:
scaled_tr1 = translate_1 / np.bincount(idx1)[idx1,None]
np.add.at(mesh_vectors, idx1, scaled_tr1)
请注意,使用np.add.at
而不是花哨的索引
ufunc.at(a,指数,b=无)
对索引指定的元素的操作数a
执行无缓冲就地操作。对于添加的ufunc,此方法相当于a[索引]+=b
,不同之处在于对索引多次的元素进行结果累加。例如,a[[0,0]+=1
由于缓冲作用只会增加第一个元素一次,而add.at(a[0,0],1)
会增加第一个元素两次
这项工作:
scaled_tr1 = translate_1 / np.bincount(idx1)[idx1,None]
np.add.at(mesh_vectors, idx1, scaled_tr1)
请注意,使用np.add.at
而不是花哨的索引
ufunc.at(a,指数,b=无)
对索引指定的元素的操作数a
执行无缓冲就地操作。对于添加的ufunc,此方法相当于a[索引]+=b
,不同之处在于对索引多次的元素进行结果累加。例如,a[[0,0]+=1
由于缓冲作用只会增加第一个元素一次,而add.at(a[0,0],1)
会增加第一个元素两次
你能用循环(或伪代码)来写它,这样我们就能更清楚地看到你的要求吗?你能用循环(或伪代码)来写它,这样我们就能更清楚地看到你的要求吗?