cumsum — Compute cumulative sums in MATLAB and RunMat.
cumsum(X) computes running sums along a chosen dimension. The output has the same size as X, with each element storing the cumulative total under MATLAB semantics.
Syntax
B = cumsum(A)
B = cumsum(A, dim)
B = cumsum(A, direction)
B = cumsum(A, nanflag)
B = cumsum(A, dim, direction)
B = cumsum(A, direction, dim)All supported cumsum forms
B = cumsum(A)
B = cumsum(A, dim)
B = cumsum(A, direction)
B = cumsum(A, nanflag)
B = cumsum(A, dim, direction)
B = cumsum(A, direction, dim)
B = cumsum(A, dim, nanflag)
B = cumsum(A, nanflag, dim)
B = cumsum(A, direction, nanflag)
B = cumsum(A, nanflag, direction)
B = cumsum(A, dim, direction, nanflag)
B = cumsum(A, dim, nanflag, direction)
B = cumsum(A, direction, dim, nanflag)
B = cumsum(A, direction, nanflag, dim)
B = cumsum(A, nanflag, dim, direction)
B = cumsum(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 |
|---|---|---|
B | NumericArray | Cumulative sum result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cumsum:InvalidArgument | Dimension, direction, or missing-value argument grammar is invalid. | cumsum: invalid argument |
RunMat:cumsum:InvalidInput | Input value type is unsupported for cumulative sum reduction. | cumsum: invalid input |
RunMat:cumsum:Internal | Reduction execution fails due to conversion, provider, or allocation operations. | cumsum: internal reduction failure |
How cumsum works
- By default, the running total is taken along the first dimension whose length is greater than 1.
cumsum(X, dim)lets you pick the dimension explicitly; ifdim > ndims(X), the input is returned unchanged.- Passing
[]for the dimension argument keeps the default dimension (MATLAB uses this as a placeholder). cumsum(..., "reverse")works from the end toward the beginning, whereas"forward"(default) works from start to finish.cumsum(..., "omitnan")treatsNaNvalues as missing. LeadingNaNvalues yield zeros until a valid number appears.- Synonyms such as
"omitmissing"/"includemissing"are also accepted for MATLAB compatibility. - The function supports real or complex scalars and dense tensors. Logical inputs are promoted to double precision.
Does RunMat run cumsum on the GPU?
When a tensor already lives on the GPU, RunMat asks the active acceleration provider for a device-side prefix-sum implementation. The WGPU provider ships a native scan kernel; other providers may still fall back. If no hook is available, RunMat gathers the data to host memory, performs the cumulative sum on the CPU, and returns a dense tensor value. Residency metadata is cleared so later operations can re-promote the tensor when profitable.
GPU memory and residency
Manual gpuArray calls are optional. RunMat promotes tensors automatically when the planner predicts a benefit, and it keeps fused expressions resident on the device. Explicit gpuArray is still supported for MATLAB compatibility or when you want to guarantee GPU residency before entering a critical loop.
Examples
Running totals down each column (default dimension)
A = [1 2 3; 4 5 6];
columnTotals = cumsum(A)Expected output:
columnTotals =
1 2 3
5 7 9Tracking cumulative sums across rows
A = [1 2 3; 4 5 6];
rowTotals = cumsum(A, 2)Expected output:
rowTotals =
1 3 6
4 9 15Reversing the direction of accumulation
v = [1 3 5 7];
reverseTotals = cumsum(v, "reverse")Expected output:
reverseTotals =
16 15 12 7Ignoring NaN values while accumulating
v = [2 NaN 5 NaN 1];
running = cumsum(v, "omitnan")Expected output:
running =
2 2 7 7 8Computing a cumulative sum inside a GPU workflow
G = gpuArray(rand(1, 5));
totals = cumsum(G);
hostResult = gather(totals)Using cumsum with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cumsum changes the result.
Run a small cumsum example, explain the result, then change one input and compare the output.
FAQ
Does cumsum change the size of the input?⌄
No. The output is always the same size as the input tensor.
What happens if I request a dimension larger than ndims(X)?⌄
The function returns X unchanged, matching MATLAB behaviour.
How are complex numbers handled?⌄
cumsum accumulates the real and imaginary parts independently. NaN checks treat a complex number as missing if either part is NaN.
What does "omitnan" do for leading NaN values?⌄
Leading NaN values contribute zeros so the running total remains 0 until a non-NaN value appears.
Does "reverse" affect which dimension is used?⌄
No. Direction only decides whether accumulation walks from the start or from the end along the selected dimension.
Can I combine "reverse" and "omitnan"?⌄
Yes. You can specify both options (in any order) and RunMat mirrors MATLAB’s results.
Does the GPU path respect "omitnan"?⌄
If the active provider does not natively handle "omitnan", RunMat gathers back to host and computes there to preserve MATLAB semantics.
Related Math functions
Reduction
all · any · cummax · cummin · cumprod · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · 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 cumsum is executed, line by line, in Rust.
- View the source for cumsum 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.