Reverse an arbitrary dimension in an ndarray

后端 未结 3 1144
慢半拍i
慢半拍i 2021-02-20 04:28

I\'m working with an n-dimensional array, and I\'d like a way to reverse a numbered dimension. So rather than

rev = a[:,:,::-1]

I\'d like to be

3条回答
  •  [愿得一人]
    2021-02-20 04:42

    For anyone coming across this in the future:

    Numpy 1.12+ has the function np.flip(array, dimension), which does exactly as requested. Even better, it returns a view of the data rather than a copy, and so it happens in constant time.

提交回复
热议问题