2

I have a 4D np.array of shape (i,j,k,l)and two 1D np.array contain the indexes in the k-axis and l-axis.

For example

idx = np.array([0, 1, 0])
idy = np.array([0, 1, 0])
array = np.arange(0, 16).reshape(2, 2, 2, 2)
array0 = array[0]
array0[:,idx,idy].shape # (2, 3)
array[0,:,idx,idy].shape # (3, 2)

I am wondering why two different indexing will lead to different shapes?

fatpanda2049
  • 141
  • 7
  • I started to write an answer, then I realized I don't actually understand it completely myself. It has to do with the number of elements used for indexing, and broadcasting of the `idx` and `idy` arrays when indexing, but how exactly I'm not sure yet. I suspect the answer will be somewhere in there: https://numpy.org/doc/stable/user/basics.indexing.html Edit: Also, `array0[:,idx,idy]` is the transpose of `array[0,:,idx,idy]`, so that's worth noting you do get the same values in the end, but not aranged in the same way. – Jenny Feb 10 '22 at 11:23
  • 1
    The explanation at the linked duplicate is not terribly clear. If you read [this section of the indexing docs](https://numpy.org/doc/stable/user/basics.indexing.html#combining-advanced-and-basic-indexing) it explains that `arr[:, idx, idy]` has all advanced indices next to each other, whereas `arr[0, :, idx, idy]` has them separated by the colon (slice), because the `0` index counts as an advanced index in this regard. – Andras Deak -- Слава Україні Feb 10 '22 at 15:14

0 Answers0