## Rotation and flipping of multidimensional numpy matrices

Question

I have a numpy matrix 'mat', which is 888 * 100 * 100. This represents 888 samples of 100 by 100 grids.

I want to apply a transformation to **each grid** in the matrix.

I tried mod_mat = np.rot90(mat), but it changed the dimension to 100 * 888 * 100.

Is there a way to carry out the transformations in one go? Or, do I need to iterate over each grid and transform it separately? Thanks.

Show source

## Answers to Rotation and flipping of multidimensional numpy matrices ( 2 )

np.rot90 rotates first two dimensions, so you can swap axes 0 and 2, rotate and swap back to achieve what you want. Python iteration probably will be slower, because numpy is a highly optimized low-level code library, so it is usually better to use built-ins.

Here's an approach using

`transpose`

and`flipping/reversing`

the last axis -Runtime test -