flip — Reverse element order along dimensions in MATLAB and RunMat.
flip(A) reverses the order of elements in A along the first nonsingleton dimension, while flip(A, dim) reverses one positive integer-valued scalar dimension. Numeric dimension vectors, typed-integer or logical dimensions, and direction keywords are separately gated RunMat-only extensions.
Syntax
B = flip(A)
B = flip(A, dim_or_direction)Inputs
A- Type
Any- Required
- Yes
- Description
- Input array/value to reverse.
dim_or_direction- Type
Any- Required
- Yes
- Description
- Dimension index/vector or direction keyword ('horizontal', 'vertical', 'both').
Returns
B- Type
Any- Description
- Input array with selected dimensions reversed.
Errors
RunMat:flip:TooManyInputs- When
- More than one optional argument is provided after A.
- Message
flip: too many input arguments
RunMat:flip:InvalidInput- When
- Input type, dimension argument, or direction token is invalid.
- Message
flip: invalid input argument
RunMat:flip:UnsupportedInput- When
- Input type is unsupported for flip.
- Message
flip: unsupported input type
Behavior and placement
flip(A) locates the first non-singleton dimension and reverses that axis.
flip(A, dim) flips along the dimension dim (1-based, MATLAB-compatible).
RunMat extension: flip(A, dims) accepts a nonempty row or column vector of unique positive dimensions.
RunMat extension: typed-integer and logical dimensions are exact structural controls; strict compatibility mode rejects them before parsing.
RunMat extension: flip(A, 'horizontal') and flip(A, 'vertical') map to dimensions 2 and 1, while 'both' flips the first two dimensions.
All eight integer data classes preserve their exact storage and output class.
Dimensions larger than ndims(A) are allowed. RunMat pads trailing singleton axes so the request becomes a no-op, matching MATLAB semantics.
Reversing an empty dimension (extent zero) is a no-op.
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
An eligible resident floating or logical value can use the owning provider's flip hook. Unsupported hooks and native integer storage use an exact gather, host reorder, and owner-provider upload when restoration is available. This is a structural fusion boundary, not an elementwise shader operation.
Examples
Reversing a Row Vector
row = 1:5;
rev = flip(row)Expected output:
rev = [5 4 3 2 1]Reversing Matrix Rows (Vertical Flip)
A = [1 2 3; 4 5 6; 7 8 9];
B = flip(A)Expected output:
B =
7 8 9
4 5 6
1 2 3Flipping Columns with the Horizontal Direction Keyword
A = magic(3);
H = flip(A, 'horizontal')Expected output:
H =
6 1 8
7 5 3
2 9 4Flipping Along Multiple Dimensions
T = reshape(1:8, [2 2 2]);
F = flip(T, [1 3])Expected output:
F(:,:,1) =
6 8
5 7
F(:,:,2) =
2 4
1 3Flipping Character Data
C = ['r','u','n'; 'm','a','t'];
Ct = flip(C, 'horizontal')Expected output:
Ct =
'nur'
'tam'Flipping gpuArray Data While Staying on the Device
G = gpuArray(rand(4, 4));
V = flip(G, 'vertical');
isgpuarray(V)Expected output:
ans = logical 1Using flip with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how flip changes the result.
Run a small flip example, explain the result, then change one input and compare the output.
FAQ
Which dimension does flip(A) use by default?⌄
The first dimension whose size is greater than one. Column vectors flip along dimension 1, row vectors flip along dimension 2.
What happens if I pass a dimension larger than ndims(A)?⌄
Nothing changes; RunMat pads missing axes with singleton dimensions, so the flip becomes a no-op, just like MATLAB.
Do direction keywords work with any array type?⌄
They work with supported flip inputs, not cell or object arrays. 'horizontal' maps to dimension 2, 'vertical' to dimension 1, and 'both' selects both axes; the extension must be enabled.
Can I flip logical or string arrays?⌄
Absolutely. Logical results stay logical, and string arrays preserve string elements while reordering their positions.
Does flipping modify gpuArray data in place?⌄
No. A nontrivial resident flip returns an independently validated result. An empty dimension selection is an identity and can retain the original handle.
What about complex inputs?⌄
Complex scalars and arrays are reversed just like real data. Scalars remain complex scalars after the operation.
Related Array functions
Shape
blkdiag · cat · circshift · diag · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · 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 flip is executed, line by line, in Rust.
- View the source for flip 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.