rot90 — Rotate matrices and N-D arrays by 90-degree increments with MATLAB-compatible direction rules.
rot90(A) rotates A by 90 degrees counterclockwise, and an optional second argument specifies additional quarter-turns (positive for counterclockwise, negative for clockwise). For N-D arrays, rotation is applied to the first two dimensions while trailing dimensions are preserved.
Syntax
B = rot90(A)
B = rot90(A, k_or_direction)Inputs
A- Type
Any- Required
- Yes
- Description
- Input matrix or N-D array.
k_or_direction- Type
Any- Required
- Yes
- Description
- Rotation count K or direction string ('clockwise'/'counterclockwise').
Returns
B- Type
Any- Description
- Array rotated by multiples of 90 degrees around dimensions 1 and 2.
Errors
RunMat:rot90:TooManyInputs- When
- More than one optional rotation argument is provided.
- Message
rot90: too many input arguments
RunMat:rot90:InvalidRotation- When
- Rotation count/direction argument is invalid.
- Message
rot90: invalid rotation argument
RunMat:rot90:UnsupportedInput- When
- Input value type is unsupported.
- Message
rot90: unsupported input type
RunMat:rot90:Internal- When
- Internal rotation or provider fallback path fails.
- Message
rot90: internal operation failed
Behavior and placement
Default behaviour rotates 90 degrees counterclockwise (k = 1).
rot90(A, K) rotates by K * 90°; the rotation count can be positive, negative, or zero. Any integer multiple of four leaves the input unchanged.
The documented numeric K form accepts scalar integer values. RunMat mode also accepts the case-insensitive direction keywords 'clockwise' and 'counterclockwise'; strict MATLAB mode rejects that extension with RunMat:compatibility:Rot90DirectionTokenExtension.
Dense integer arrays retain their exact native class and values through every rotation, including uint64 values above the binary64 exactness limit.
Works for numeric tensors, logical masks, complex arrays, string arrays, and character matrices. Scalars are unchanged.
For empty dimensions the function still swaps the first two extents, so rot90(zeros(0, 3)) returns a 3×0 array.
Numeric reordering preserves native single, double, logical, complex, and all eight integer storage classes where supported. Structure arrays are reordered by their owning structural path; cell arrays are supported only by flipud.
Explicit gpuArray inputs remain with their exact owner when the provider can execute or restore the result; automatic device placement may restore to host. Controls are validated before provider effects.
GPU execution and fusion
The current resident floating route composes the owning provider's permute and flip hooks; no dedicated rot90 hook is dispatched. Native integer and other unsupported direct classes use an exact owner-controlled fallback. This is a structural fusion boundary.
Examples
Rotating a matrix 90 degrees counterclockwise
A = [1 2 3; 4 5 6];
B = rot90(A)Expected output:
B =
3 6
2 5
1 4Rotating a matrix clockwise using a direction keyword
A = magic(3);
C = rot90(A, 'clockwise')Expected output:
C =
4 3 8
9 5 1
2 7 6Applying multiple 90-degree turns with a numeric count
A = reshape(1:9, [3 3]);
B = rot90(A, 2); % 180-degree rotationExpected output:
B =
9 6 3
8 5 2
7 4 1Rotating 3-D data while preserving trailing dimensions
T = reshape(1:12, [2 3 2]);
R = rot90(T);
size(R)Expected output:
ans =
3 2 2Rotating character data to reorganise text
C = ['r','u','n'; 'm','a','t'; ' ','A','I'];
R = rot90(C)Expected output:
R =
'ntI'
'uaA'
'rm 'Rotating gpuArray data and keeping it on the device
G = gpuArray(reshape(1:9, [3 3]));
H = rot90(G, -1); % rotate clockwise
isgpuarray(H)Expected output:
ans = logical 1Using rot90 with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how rot90 changes the result.
Run a small rot90 example, explain the result, then change one input and compare the output.
FAQ
What directions does rot90 support?⌄
The compatibility surface uses an integer rotation count. RunMat mode also recognizes 'clockwise' and 'counterclockwise'; strict MATLAB mode rejects those direction tokens as a RunMat extension.
Does rot90 modify dimensions beyond the first two?⌄
No. Only the first two axes participate in the rotation. Other dimensions keep their order and extents unchanged.
What happens for empty matrices?⌄
Empty inputs still swap the first two dimension sizes. For example, a 0×5 matrix becomes 5×0 after a single counterclockwise rotation.
Can I rotate logical, string, or character arrays?⌄
Yes. Logical results remain logical, string arrays preserve their elements, and character matrices rotate their characters exactly like numeric data.
How do large rotation counts behave?⌄
The rotation count is reduced modulo 4. Values such as rot90(A, 37) behave the same as rot90(A, 1).
Is rot90 compatible with complex numbers?⌄
Absolutely. Complex tensors rotate without altering the real or imaginary parts; only the element positions change.
Can providers implement a custom kernel?⌄
The current runtime composes resident floating rotation through the owning provider's permute and flip hooks, and uses an exact owner-controlled fallback for other storage. It does not dispatch a dedicated rot90 hook.
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how rot90 is executed, line by line, in Rust.
- View the source for rot90 in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.