Let's say I have an numpy array A of size n x m x k and another array B of size n x m that has indices from 1 to k. I want to access each n x m slice of A using the index given at this place in B, giving me an array of size n x m.
Edit: that is apparently not what I want!
[[ I can achieve this using take
like this:
A.take(B)
]] end edit
Can this be achieved using fancy indexing?
I would have thought A[B]
would give the same result, but that results
in an array of size n x m x m x k (which I don't really understand).
The reason I don't want to use take
is that I want to be able to assign this portion something, like
A[B] = 1
The only working solution that I have so far is
A.reshape(-1, k)[np.arange(n * m), B.ravel()].reshape(n, m)
but surely there has to be an easier way?