How do you flip each dimension in a NumPy array sequentially?

I found in MATLAB the following function that sequentially flips all sizes in a matrix:

function X=flipall(X)
    for i=1:ndims(X)
        X = flipdim(X,i);
    end
end

Where Xit measures (M,N,P) = (24,24,100). How can I do this in Python, given what Xis a NumPy array?

+4
source share
1 answer

Equivalent flipdimin MATLAB flip's numpy. Keep in mind that this is only available in version 1.12.0.

Therefore, it is simple:

import numpy as np

def flipall(X):
    Xcopy = X.copy()
    for i in range(X.ndim):
        Xcopy = np.flip(Xcopy, i)
     return Xcopy

So you simply name it like this:

Xflip = flipall(X)

However, if you know a priori that you have only three dimensions, you can hard code the operation by simply doing:

def flipall(X):
    return X[::-1,::-1,::-1]

.


1.12.0 ( hpaulj), slice :

import numpy as np

def flipall(X):
    return X[[slice(None,None,-1) for _ in X.shape]]
+5

Source: https://habr.com/ru/post/1654653/


All Articles