1

So I am trying to refresh my knowledge. I tried finding the answer to this question here and I am not still not sure how to answer this.

Basically if I have a 2D index array (call it a) of shape (x, z) and a 3D array (call it b) of shape (x, y, z), then I want a 2D array (call it c) which has the property that the i,j element of c, c[i, j] = b[i, a[i, j], j]. Shape of c is (x, z)

To summarize:
a.shape = (x, z)
b.shape = (x, y, z)   
c[i, j] = b[i, a[i, j], j]
c.shape = (x, z)

I tried to use the following code, but did not get the results I desire.

import numpy as np

input = numpy.random.randn(3, 5, 10)
target_np = np.random.randint(high = 5, low = 1, size = (3, 10))
q = input[:, np.ravel(target_np),:]

print(q.shape)

The result was

(3, 30, 10)
user278039
  • 139
  • 6

0 Answers0