What does the fliplr function do in MATLAB / RunMat?
fliplr(A) mirrors A across its vertical axis, reversing the column order (dimension 2).
It accepts scalars, vectors, matrices, N-D tensors, logical arrays, character arrays,
string arrays, complex data, and gpuArray handles, matching MATLAB semantics.
How does the fliplr function behave in MATLAB / RunMat?
- Always reverses dimension 2 (columns) and leaves all other dimensions untouched, even for rank > 2 data.
- Inputs with fewer than two columns (column vectors, scalars) are returned unchanged because the second dimension is singleton.
- Logical, numeric, complex, character, and string arrays all preserve their MATLAB types and storage layout.
- gpuArray inputs execute on the device via the generic
flipprovider hook (axis = 1); when that hook is missing, RunMat gathers once, mirrors the data on the host, and uploads the result so the returned value is still a gpuArray. - Dimensions larger than
ndims(A)are treated as singleton axes, sofliplrnever errors whenAhas rank < 2.
fliplr Function GPU Execution Behaviour
RunMat first tries to execute fliplr on the GPU by delegating to the provider’s generic flip
implementation with axis 1 (zero-based). If the provider does not implement this hook, RunMat
transparently gathers the tensor, performs the horizontal flip on the host, and uploads the result
back to the device so residency is preserved.
Examples of using the fliplr function in MATLAB / RunMat
Reverse Columns of a Matrix
A = [1 2 3; 4 5 6];
B = fliplr(A);
Expected output:
B =
3 2 1
6 5 4
Mirror an Image Matrix Horizontally
img = reshape(1:16, 4, 4);
mirrored = fliplr(img);
Expected output:
mirrored =
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13
Flip the Order of Polynomial Coefficients
c = [1 0 -2 5];
rev = fliplr(c);
Expected output:
rev = [5 -2 0 1];
Reverse Each Slice in a 3-D Array Along Columns
T = reshape(1:24, [3 4 2]);
F = fliplr(T);
Expected output:
F(:,:,1) =
4 3 2 1
8 7 6 5
12 11 10 9
F(:,:,2) =
16 15 14 13
20 19 18 17
24 23 22 21
Preserve Column Vector Orientation
col = (1:4)';
same = fliplr(col);
Expected output:
same =
1
2
3
4
Flip Characters in a Two-Row Char Array
C = ['r','u','n'; 'm','a','t'];
Ct = fliplr(C);
Expected output:
Ct =
'nur'
'tam'
Keep gpuArray Results on the Device While Flipping Columns
G = gpuArray(rand(8, 8));
H = fliplr(G);
Expected workflow:
isa(H, 'gpuArray')
GPU residency in RunMat (Do I need gpuArray?)
You typically do not need to call gpuArray directly. RunMat’s auto-offload planner keeps tensors on
the GPU when profitable and only gathers when a provider lacks the flip hook. Even in that fallback,
fliplr uploads the flipped result back to the device so subsequent operations can stay gpu-resident.
FAQ
Does fliplr change column vectors?
No. A column vector has a singleton second dimension, so reversing that axis leaves the data unchanged.
Is fliplr the same as calling flip(A, 2)?
Yes. fliplr is a convenience wrapper around flip that always targets dimension 2 (columns).
Can I apply fliplr to N-D tensors?
Absolutely. Only dimension 2 is reversed; all other axes keep their original order regardless of rank.
Does fliplr support string and character arrays?
Yes. String arrays reorder their elements, and character arrays mirror each row while preserving UTF-8 data.
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 fliplr allocate new GPU buffers?
Providers may reuse storage, but the builtin always returns a fresh handle. The simple provider uploads a new buffer.
Is fliplr numerically stable?
Yes. The function only reorders elements; values are never modified, so it is numerically stable.
See Also
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/array/shape/fliplr.rs - Found a behavioural difference? Open an issue with details and a minimal repro.