Skip to content
Advertisement

Eliminate for loop when indexing into array

I have two arrays: vals has shape (N,m) where N is ~1 million, and m is 3. The values are floats I have another array indices with shape (N,4). All values in indices are row indices in vals. (Additionally, unlike the example here, every row of indices contains unique values.).

JavaScript

I would like replace the following for loop when creating the array aug

JavaScript

The broader context for the question is vals contains numeric data for particles distributed in 3D. indices is generated using a nearest neighbor search on the spatial positions of the particles (using scipy.spatial.cKDTree) . I would like to average the numeric data over the nearest neighbors. As I have ~1 million particles, a for-loop is quite slow.

Advertisement

Answer

You actually can replace the entire aug = ... line with

JavaScript

That will produce the same result:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement