flipud — Flip arrays up-to-down in MATLAB and RunMat.
flipud(A) reverses dimension 1, leaving other axes in place. Numeric, logical, complex, character, string, cell, structure-array, and gpuArray paths are implemented; object inputs are unsupported.
Syntax
B = flipud(A)Inputs
A- Type
Any- Required
- Yes
- Description
- Input array/value to reverse along rows.
Returns
B- Type
Any- Description
- Input array mirrored up-to-down (dimension 1).
Errors
RunMat:flipud:InvalidInput- When
- Input type or conversion path is invalid for flipud.
- Message
flipud: invalid input argument
RunMat:flipud:UnsupportedInput- When
- Input type is unsupported for flipud.
- Message
flipud: unsupported input type
Behavior and placement
Always reverses dimension 1 (rows) and leaves all other dimensions untouched, even for rank > 2 data.
Inputs with a single row (row vectors, scalars) are returned unchanged because the first dimension is singleton.
Numeric, logical, complex, character, string, and cell arrays retain their types and full N-D shapes. All eight integer classes preserve authoritative values and class.
Dimensions larger than ndims(A) are treated as singleton axes, so flipud never errors when A has rank < 1.
Behaviour matches flip(A, 1) exactly; flipud is provided for readability and compatibility with existing MATLAB code.
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 resident route uses the owning provider's flip hook on zero-based axis 0 where supported. Other classes use an exact owner-controlled gather and upload fallback. This is a structural fusion boundary.
Examples
Reverse Rows of a Matrix
A = [1 2 3; 4 5 6; 7 8 9];
B = flipud(A)Expected output:
B =
7 8 9
4 5 6
1 2 3Reverse a Column Vector
col = (1:4)';
rev = flipud(col)Expected output:
rev =
4
3
2
1Flip the First Dimension of a 3-D Tensor
T = reshape(1:24, [3 4 2]);
F = flipud(T)Expected output:
F(:,:,1) =
3 6 9 12
2 5 8 11
1 4 7 10
F(:,:,2) =
15 18 21 24
14 17 20 23
13 16 19 22Flip Characters in a Char Array Vertically
C = ['run'; 'mat'];
Cv = flipud(C)Expected output:
Cv =
'mat'
'run'Preserve Row Vector Orientation
row = 1:5;
same = flipud(row)Expected output:
same = [1 2 3 4 5]Keep gpuArray Results on the Device While Flipping Rows
G = gpuArray(rand(8, 8));
H = flipud(G);
isequal(gather(H), flipud(gather(G))) % illustrative verificationUsing flipud with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how flipud changes the result.
Run a small flipud example, explain the result, then change one input and compare the output.
FAQ
Does flipud change row vectors?⌄
No. A row vector has a singleton first dimension, so reversing that axis leaves the data unchanged.
Is flipud the same as calling flip(A, 1)?⌄
Yes. flipud targets dimension 1 (rows).
Can I apply flipud to N-D tensors?⌄
Yes. Only dimension 1 is reversed; all other axes keep their original order regardless of rank.
Does flipud support string, character, and cell arrays?⌄
Yes. String arrays reorder their elements, character arrays preserve their character storage, and cell arrays reverse their rows without changing contained values.
What happens on the GPU if there is no flip kernel?⌄
RunMat gathers the tensor once, mirrors it on the CPU, and uploads the result so you still receive a gpuArray.
Does flipud allocate new GPU buffers?⌄
The resident route validates an independent output handle; the owning provider determines the physical backing. The input is not modified.
Is flipud numerically stable?⌄
Yes. The function only reorders elements; values are never modified, so it is numerically stable.
What does flipud do in MATLAB?⌄
— B = flipud(A) returns A with the order of its rows reversed, so B(i, :) = A(end - i + 1, :). The number of columns and every higher-dimension page stays exactly the same.
What's the difference between flipud, fliplr, and flip?⌄
flipud reverses dimension 1 (rows), fliplr reverses dimension 2 (columns), and flip(A, dim) reverses a selected dimension. The named forms remain useful for readability.
How do I reverse a column vector?⌄
— For a column vector, flipud(v) returns the reversed vector. A dimension-agnostic idiom that works for row or column vectors is v(end:-1:1).
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · 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 flipud is executed, line by line, in Rust.
- View the source for flipud 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.