fliplr — Flip an array left-to-right along the second dimension.
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 fliplr works in 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.
How fliplr runs on the GPU
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.
GPU memory and residency
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.
Examples
Reverse Columns of a Matrix
A = [1 2 3; 4 5 6];
B = fliplr(A)Expected output:
B =
3 2 1
6 5 4Mirror 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 13Flip 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 21Preserve Column Vector Orientation
col = (1:4)';
same = fliplr(col)Expected output:
same =
1
2
3
4Flip 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)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.
Related functions to explore
These functions work well alongside fliplr. Each page has runnable examples you can try in the browser.
flip, flipud, permute, reshape, gpuArray, gather, cat, circshift, diag, horzcat, ipermute, kron, repmat, rot90, squeeze, tril, triu, vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fliplr works, line by line, in Rust.
- View fliplr.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.