Assume I have an array
>>> a
[[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]]
that I want to flip around an axis to end up with
>>> aflipped
[[[2, 1, 0], [5, 4, 3], [8, 7, 6]], [[12, 11, 10], [15, 14, 13], [18, 17, 16]]]
I would like to do this with some kind of the
>>> aflipped=a[::-1][::1][::1]
or
>>>> aflipped=flipud(a)
notation, since I understand this is extremely fast and (important) low on memory usage. My code ends up swapping already, a for loop wouldn't bee ideal at all.
Actually this is a 4D array where I just want to flip one axis, but it seems my options are limited to the first two axis. Is there a memory efficient method to do so?
Something like this:
>>> a = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
>>> a[:,:,::-1] #or a[..., ::-1]
array([[[ 2, 1, 0],
[ 5, 4, 3],
[ 8, 7, 6]],
[[12, 11, 10],
[15, 14, 13],
[18, 17, 16]]])
Timing comparisons:
>>> %timeit a[:,:,::-1]
1000000 loops, best of 3: 1.53 µs per loop
>>> %timeit a[..., ::-1]
1000000 loops, best of 3: 1.06 µs per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With