Search code examples
pythonarraysnumpynumpy-ndarray

What is the difference between flip() and flipud() in NumPy?


What is the difference between flip() and flipud() in NumPy?

Both functions do the same things so which one should I use?


Solution

  • Both functions do the same things

    That's only true for 1-D arrays.

    For N-D arrays, np.flipud(a) is only the same as np.flip(a, axis=0). There's another convenience function np.fliplr(a) that corresponds to np.flip(a, axis=1).


    np.flip is required if you need to flip dimension 3 or higher, e.g., the 3rd dimension (axis 2):

    np.flip(a, axis=2)
    

    Or to flip multiple dimensions at once, e.g., the 1st and 3rd dimensions (axis 0 and 2):

    np.flip(a, axis=(0, 2))