rot90

Rotates a matrix by multiples of 90 degrees.

Category: Language Fundamentals > Matrices and Arrays

Syntax

B = rot90(A)

Rotates 90 degrees counterclockwise.

B = rot90(A, k)

Rotates k*90 degrees; negative k turns clockwise.

Description

The first row of A ends up as the first column of B, read from bottom to top, after one counterclockwise turn. Only k modulo 4 matters. For a half turn use rot90(A, 2), which equals flip(flip(A, 1), 2).

Input arguments

A
Array of any type.
k
Integer number of quarter turns (default 1).

Output arguments

B
Rotated array. For odd k its rows and columns are swapped.

Example

A = [1 2; 3 4];
rot90(A)
rot90(A, -1)
rot90(A, 2)
Output
ans = 2×2

     2     4
     1     3
ans = 2×2

     3     1
     4     2
ans = 2×2

     4     3
     2     1

See also

flip fliplr flipud permute circshift