I have a 3d array as below:
import numpy an np
arr3D = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],\
[[13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]]])
or
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]]]
I want to re-order my rows such as below:
[[[10 11 12]
[ 7 8 9]
[ 4 5 6]
[ 1 2 3]]
[[22 23 24]
[19 20 21]
[16 17 18]
[13 14 15]]]
I guess one way of doing that is using .argsort()
but I am confused if that is possible to do that or I need to use other strategies?
CodePudding user response:
You can use ::-1
for reversing on each axis that you like.
>>> arr3D[:, ::-1, :] # <- you want this, reverse on axis=1
array([[[10, 11, 12],
[ 7, 8, 9],
[ 4, 5, 6],
[ 1, 2, 3]],
[[22, 23, 24],
[19, 20, 21],
[16, 17, 18],
[13, 14, 15]]])
# Another example, reverse on axis=2 or axis=-1
>>> arr3D[...,::-1]
array([[[ 3, 2, 1],
[ 6, 5, 4],
[ 9, 8, 7],
[12, 11, 10]],
[[15, 14, 13],
[18, 17, 16],
[21, 20, 19],
[24, 23, 22]]])