本文介绍了制作用于数组索引循环更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:我有数组索引具有重复指数和希望值添加到一个这样的数组:
I have the following problem: I have index arrays with repeating indices and would like to add values to an array like this:
grid_array[xidx[:],yidx[:],zidx[:]] += data[:]不过,正如我反复指标,这并不因为它应该因为numpy的会创建一个临时数组被分配反复指标,而不是被添加到对方多次导致的数据(见的 docs.scipy/doc/numpy/user/basics.indexing.html )。
一个for循环像
for i in range(0,n): grid_array[xidx[i],yidx[i],zidx[i]] += data[i]便一路放缓。有没有一种方法,我仍然可以使用numpy的的矢量化?还是有另一种方法,使这项任务更快?
will be way to slow. Is there a way I can still use the vectorization of numpy? Or is there another way to make this assignment faster?
感谢您的帮助。
推荐答案如何使用bincount?
How about using bincount?
import numpy as np flat_index = np.ravel_multi_index([xidx, yidx, zidx], grid_array.shape) datasum = np.bincount(flat_index, data, minlength=grid_array.size) grid_array += datasum.reshape(grid_array.shape)更多推荐
制作用于数组索引循环更快
发布评论