1

I have two numpy arrays of the following shapes, and I need to extract values from a 3D array using stored indices from a 2D one:

  • vals = (65, 65, 3500) This contains 3500 values that were calculated in a 65x65 grid, from which I need to extract values
  • indices = (65, 65) This contains the indices of the minimum value of each of the 3500 of the previous array, of which I need to extract the values

The resulting array should again be (65, 65).

For example, if I have a value 1367 stored in indices[0,0], then I need vals[0,0,1367]. This needs to be repeated all the way to indices[64,64].

How can I do this sort of filter? I tried np.choose with reshaping vals to (3500, 65, 65), but that is limited to 32 columns and therefore crashed. Alternatively, how can I get the (65, 65) array of the minima of each set of 3500 values? Thanks

1
  • 1
    vals[np.arange(65)[:, None], np.arange(65)[None], indices] or vals[(*np.indices(vals.shape[:-1], sparse=True), indices)] Commented Oct 13, 2022 at 3:04

2 Answers 2

1
import numpy as np
vals.reshape(-1, 3500)[np.arange(65*65), indices.ravel()].reshape(65, 65)

This simplifies the operation by only doing selection by row. On my PC this is also slightly faster than the meshgrid method.

36 µs ± 239 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

compared to

44.6 µs ± 693 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Sign up to request clarification or add additional context in comments.

1 Comment

What an elegant reply, thank you!
1

this can be done using a meshgrid

import numpy as np

vals = np.random.rand(65,65,300)
indices  = np.random.randint(0,300,[65,65])
mesh_grid = np.meshgrid(np.arange(indices.shape[1]),np.arange(indices.shape[0]),sparse=True)
output = vals[mesh_grid[1],mesh_grid[0],indices]

print(output.shape)
(65, 65)

1 Comment

It works now OK after you have fixed it (deleting my first comment to avoid confusion by future visitors).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.