7

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?

0

1 Answer 1

13

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.