cummax — Compute cumulative maxima (with optional indices) in MATLAB and RunMat.
cummax(X) computes running maxima along a chosen dimension. Optional second output returns indices of each running maximum, with behavior aligned to MATLAB and RunMat.
Syntax
M = cummax(A)
M = cummax(A, dim)
M = cummax(A, direction)
M = cummax(A, nanflag)
M = cummax(A, dim, direction)
M = cummax(A, direction, dim)All supported cummax forms
M = cummax(A)
M = cummax(A, dim)
M = cummax(A, direction)
M = cummax(A, nanflag)
M = cummax(A, dim, direction)
M = cummax(A, direction, dim)
M = cummax(A, dim, nanflag)
M = cummax(A, nanflag, dim)
M = cummax(A, direction, nanflag)
M = cummax(A, nanflag, direction)
M = cummax(A, dim, direction, nanflag)
M = cummax(A, dim, nanflag, direction)
M = cummax(A, direction, dim, nanflag)
M = cummax(A, direction, nanflag, dim)
M = cummax(A, nanflag, dim, direction)
M = cummax(A, nanflag, direction, dim)
[M, I] = cummax(A)
[M, I] = cummax(A, dim)
[M, I] = cummax(A, direction)
[M, I] = cummax(A, nanflag)
[M, I] = cummax(A, dim, direction)
[M, I] = cummax(A, direction, dim)
[M, I] = cummax(A, dim, nanflag)
[M, I] = cummax(A, nanflag, dim)
[M, I] = cummax(A, direction, nanflag)
[M, I] = cummax(A, nanflag, direction)
[M, I] = cummax(A, dim, direction, nanflag)
[M, I] = cummax(A, dim, nanflag, direction)
[M, I] = cummax(A, direction, dim, nanflag)
[M, I] = cummax(A, direction, nanflag, dim)
[M, I] = cummax(A, nanflag, dim, direction)
[M, I] = cummax(A, nanflag, direction, dim)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar or array. |
dim | Any | No | [] | Dimension selector (placeholder [] keeps default dimension). |
direction | StringScalar | No | "forward" | Scan direction: "forward" or "reverse". |
nanflag | StringScalar | No | "includenan" | Missing-value mode: "includenan"/"includemissing" or "omitnan"/"omitmissing". |
Returns
| Name | Type | Description |
|---|---|---|
M | NumericArray | Cumulative maximum values. |
I | NumericArray | One-based running maximum indices along the reduction dimension. |
Returned values from cummax depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cummax:InvalidArgument | Dimension, direction, or missing-value argument grammar is invalid. | cummax: invalid argument |
RunMat:cummax:InvalidInput | Input value type is unsupported for cumulative maximum reduction. | cummax: invalid input |
RunMat:cummax:Internal | Reduction execution fails due to conversion, provider, or allocation operations. | cummax: internal reduction failure |
How cummax works
- By default the running maximum follows the first non-singleton dimension. Use
cummax(X, dim)to choose a dimension explicitly; ifdim > ndims(X), the input is returned unchanged and the indices are ones. [Y, I] = cummax(X, ...)returns both the running maxima (Y) and the indices of where those maxima were observed (I). Indices are one-based and match MATLAB exactly.- Add
"reverse"(or"forward") to control the scan direction. Reverse mode walks from the end of the chosen dimension back to the beginning. "omitnan"skipsNaNvalues when choosing the running maximum, returningNaNonly when every value seen so far isNaN."includenan"(default) propagatesNaNonce one is encountered.- Synonyms such as
"omitmissing"/"includemissing"are accepted for MATLAB compatibility. - Real and complex inputs are supported. Complex numbers are ordered using MATLAB's magnitude-and-angle rules.
Does RunMat run cummax on the GPU?
When the input already resides on the GPU, RunMat calls the acceleration provider's cummax_scan hook. Providers that implement this hook return GPU handles for both the running maxima and their indices. If the hook is missing—or if it rejects the requested options (such as "omitnan" or "reverse")—RunMat gathers the data to the host, computes the MATLAB-compatible result, and returns dense tensors on the CPU. Residency metadata is cleared so downstream kernels can re-promote values when profitable.
GPU memory and residency
Manual gpuArray calls are optional. The planner keeps tensors on the GPU when profitable, and the cummax builtin preserves residency whenever the provider implements cummax_scan. If the hook is unavailable, RunMat gathers to the host transparently and still returns MATLAB-compatible maxima and indices. You can still use gpuArray to match MATLAB scripts or force residency ahead of a tight GPU loop.
Examples
Tracking column-wise running maxima
A = [4 2 7; 3 5 1];
[Y, I] = cummax(A);
disp(Y);
disp(I)Expected output:
Y =
4 2 7
4 5 7
I =
1 1 1
1 2 1Requesting running maxima across rows
A = [4 2 7; 3 5 1];
[Y, I] = cummax(A, 2);
disp(Y);
disp(I)Expected output:
Y =
4 4 7
3 5 5
I =
1 1 3
1 2 2Getting cumulative maxima in reverse order
v = [8 3 6 2];
[Y, I] = cummax(v, "reverse")Expected output:
Y = [8 6 6 2]
I = [1 3 3 4]Ignoring NaN values in running maxima
v = [NaN 5 NaN 3];
[Y, I] = cummax(v, "omitnan")Expected output:
Y = [NaN 5 5 5]
I = [NaN 2 2 2]Capturing running maxima and indices on the GPU
G = gpuArray([3 1 4 1 5]);
[Y, I] = cummax(G);
hostY = gather(Y)
hostI = gather(I)Expected output:
hostY = [3 3 4 4 5];
hostI = [1 1 3 3 5]Using cummax with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cummax changes the result.
Run a small cummax example, explain the result, then change one input and compare the output.
FAQ
Does cummax always return indices?⌄
Yes. The builtin produces MATLAB-compatible indices internally. When a call requests two outputs ([Y, I] = cummax(...)), I is surfaced directly. For single-output calls the indices remain available to the runtime for later retrieval.
How are complex numbers ordered?⌄
Complex maxima follow MATLAB's rules: values compare by magnitude, and ties break by phase angle. "omitnan" treats elements with NaN real or imaginary parts as missing.
What happens when all elements seen so far are NaN with "omitnan"?⌄
The running maximum stays NaN and the corresponding index is NaN until a finite value is encountered. Once a finite value appears, subsequent NaNs leave the maximum and index unchanged.
Does the "reverse" option change the reported indices?⌄
Indices are still reported using 1-based positions along the chosen dimension. "reverse" simply walks the dimension from end to start before writing the outputs.
What if the requested dimension exceeds ndims(X)?⌄
The input is returned unchanged. Every index is 1, matching MATLAB's treatment of singleton trailing dimensions.
Related Math functions
Reduction
all · any · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cummax is executed, line by line, in Rust.
- View the source for cummax in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 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.