Wajih - 11 months ago 103

Python Question

I have encountered the following function in a Matlab code set

`function X=flipall(X)`

for i=1:ndims(X)

X = flipdim(X,i);

end

end

Where X has dimensions (M,N,P ) = (24,24,100)

How can I redo this in Python? Given that I need a numpy array

Answer Source

The equivalent to `flipdim`

in MATLAB is `flip`

in `numpy`

. Be advised that this is only available in version 1.12.0.

Therefore, it's simply:

```
import numpy as np
def flipall(X):
for i in range(X.ndim):
X = np.flip(X, i)
```

Be advised that this will mutate the `numpy`

array so there's no need to return anything. As such, you'd simply call it like so:

```
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):
X = X[::-1,::-1,::-1]
```

This flips each dimension one right after the other.