Slicing a numpy array along a dynamically specified axis

前端 未结 5 1749
不思量自难忘°
不思量自难忘° 2020-11-29 10:54

I would like to dynamically slice a numpy array along a specific axis. Given this:

axis = 2
start = 5
end = 10

I want to achieve the same r

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 11:27

    As it was not mentioned clearly enough (and i was looking for it too):

    an equivalent to:

    a = my_array[:, :, :, 8]
    b = my_array[:, :, :, 2:7]
    

    is:

    a = my_array.take(indices=8, axis=3)
    b = my_array.take(indices=range(2, 7), axis=3)
    

提交回复
热议问题