View all functions

CategoryArray: Shape
GPUYes

What does the flipud function do in MATLAB / RunMat?

flipud(A) mirrors A across its horizontal axis, reversing the order of rows (dimension 1). It works with scalars, vectors, matrices, N-D tensors, logical arrays, character arrays, string arrays, cell arrays, complex data, and gpuArray handles, matching MATLAB semantics.

How does the flipud function behave in MATLAB / RunMat?

  • 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 all retain their MATLAB types, layout, and metadata (including UTF-16 code units for char arrays).
  • gpuArray inputs execute on the device via the generic flip provider hook (axis = 0); when that hook is unavailable, 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, 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.

flipud Function GPU Execution Behaviour

RunMat first tries to execute flipud on the GPU by delegating to the provider’s generic flip implementation with axis 0 (zero-based). If the provider does not implement this hook, RunMat transparently gathers the tensor, performs the vertical flip on the host, and uploads the result back to the device so residency is preserved.

Examples of using the flipud function in MATLAB / RunMat

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     3

Reverse a Column Vector

col = (1:4)';
rev = flipud(col);

Expected output:

rev =
     4
     3
     2
     1

Flip 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    22

Flip 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 verification

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, flipud uploads the flipped result back to the device so subsequent operations remain gpu-resident.

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 is a convenience wrapper around flip that always targets dimension 1 (rows).

Can I apply flipud to N-D tensors?

Absolutely. 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 mirror each column while preserving UTF-8 data, and cell arrays reverse their rows without copying 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?

Providers may reuse storage, but the builtin always returns a fresh handle. The simple provider uploads a new buffer.

Is flipud numerically stable?

Yes. The function only reorders elements; values are never modified, so it is numerically stable.

See Also

Source & Feedback