cpicanco - 1 year ago 800

Python Question

Is there a faster way of flipping and rotating an array in numpy? For example, rotating one time counter clockwise and then flipping?

`import numpy as np`

a = np.arange(0,10)

b = np.arange(-11,-1)

ar = np.array([a,b])

print ar

print ar.shape

ar = np.rot90(ar, 3)

print np.fliplr(ar)

print ar.shape

Output:

`[[ 0 1 2 3 4 5 6 7 8 9]`

[-11 -10 -9 -8 -7 -6 -5 -4 -3 -2]]

(2, 10)

[[ 0 -11]

[ 1 -10]

[ 2 -9]

[ 3 -8]

[ 4 -7]

[ 5 -6]

[ 6 -5]

[ 7 -4]

[ 8 -3]

[ 9 -2]]

(10, 2)

[Finished in 0.1s]

P.S.: This question is not a duplicate of: Transposing a NumPy array. The present question does not contest the stability of the "transpose" function; it is asking for the function itself.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

A flip and rotate together (based on your example) is a *matrix transpose*: a matrix transpose is a permutation of the matrix's dimensions: for instance the first dimension becomes the second dimension and vice versa.

numpy supports the `numpy.transpose`

function:

`numpy.transpose(a, axes=None)`

Permute the dimensions of an array.

Parameters:

`a : array_like`

: Input array.`axes`

: list of ints, optional By default, reverse the dimensions, otherwise permute the axes according to the values given.

Returns:

`p : ndarray`

: a with its axes permuted. A view is returned whenever possible.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**