numpy reverse multidimensional array

后端 未结 2 2029
忘掉有多难
忘掉有多难 2020-12-01 20:57

What is the simplest way in numpy to reverse the most inner values of an array like this:

array([[[1, 1, 1, 2],
    [2, 2, 2, 3],
    [3, 3, 3, 4]],

   [[1,         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 21:10

    How about:

    import numpy as np
    a = np.array([[[10, 1, 1, 2],
                   [2, 2, 2, 3],
                   [3, 3, 3, 4]],
                  [[1, 1, 1, 2],
                   [2, 2, 2, 3],
                   [3, 3, 3, 4]]])
    

    and the reverse along the last dimension is:

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

    or

    b = a[...,::-1]
    

    although I like the later less since the first two dimensions are implicit and it is more difficult to see what is going on.

提交回复
热议问题