Main Content

rot90

Rotate array 90 degrees

Description

B = rot90(A) rotates array A counterclockwise by 90 degrees. For multidimensional arrays, rot90 rotates in the plane formed by the first and second dimensions.

example

B = rot90(A,k) rotates array A counterclockwise by k*90 degrees, where k is an integer.

example

Examples

collapse all

Create a column vector of sequential elements.

A = (1:5)'
A = 5×1

     1
     2
     3
     4
     5

Rotate A counterclockwise by 90 degrees using rot90.

B = rot90(A)
B = 1×5

     1     2     3     4     5

The result, B, has the same elements as A but a different orientation.

Create a 3-by-3-by-2 cell array of characters.

A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' 'o';'p' 'q' 'r'})
A = 3x3x2 cell array
A(:,:,1) = 

    {'a'}    {'b'}    {'c'}
    {'d'}    {'e'}    {'f'}
    {'g'}    {'h'}    {'i'}


A(:,:,2) = 

    {'j'}    {'k'}    {'l'}
    {'m'}    {'n'}    {'o'}
    {'p'}    {'q'}    {'r'}

Rotate the cell array by 270 degrees.

B = rot90(A,3)
B = 3x3x2 cell array
B(:,:,1) = 

    {'g'}    {'d'}    {'a'}
    {'h'}    {'e'}    {'b'}
    {'i'}    {'f'}    {'c'}


B(:,:,2) = 

    {'p'}    {'m'}    {'j'}
    {'q'}    {'n'}    {'k'}
    {'r'}    {'o'}    {'l'}

The function rotates each page of the array independently. Since a full 360 degree rotation (k = 4) leaves the array unchanged, rot90(A,3) is equivalent to rot90(A,-1).

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | cell | categorical | datetime | duration | calendarDuration
Complex Number Support: Yes

Rotation constant, specified as an integer. Specify k to rotate by k*90 degrees rather than nesting calls to rot90.

Example: rot90(A,-2) rotates A by -180 degrees and is equivalent to rot90(A,2), which rotates by 180 degrees.

Tips

  • Use the flip function to flip arrays in any dimension.

  • When visualizing rotated data, the coordinate system used for plotting can impact the appearance of the rotation. For example, plotting rotated data B using the command imagesc(B) followed by the command axis xy to automatically choose the x and y axes can cause the data to appear as though it was rotated clockwise instead of counterclockwise.

Extended Capabilities

Version History

Introduced before R2006a

See Also

| |