制作用于数组索引循环更快

编程入门 行业动态 更新时间:2024-10-22 16:40:20
本文介绍了制作用于数组索引循环更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下问题:我有数组索引具有重复指数和希望值添加到一个这样的数组:

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)

更多推荐

制作用于数组索引循环更快

本文发布于:2023-10-25 13:46:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更快   数组   索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!