View all functions

CategoryMath: Reduction
GPUYes

What does the median function do in MATLAB / RunMat?

median(x) returns the middle value of scalars, vectors, matrices, and higher-dimensional tensors. When no dimension is supplied, the reduction runs along the first non-singleton dimension.

How does the median function behave in MATLAB / RunMat?

  • median(X) on an m × n matrix returns a row vector (1 × n) containing the column medians.
  • median(X, 2) returns a column vector (m × 1) containing the row medians.
  • median(X, 'all') reduces across every element in X and returns a scalar.
  • median(X, vecdim) accepts a row or column vector of dimensions (for example [1 3]) and reduces each listed axis in a single call while preserving the others.
  • Even-length slices return the average of the two center elements after sorting.
  • Logical inputs are promoted to double precision (true → 1.0, false → 0.0) before computing the median.
  • median(..., 'omitnan') ignores NaN values; if every element is NaN, the result is NaN.
  • median(..., 'includenan') (default) propagates NaN when any value in the slice is NaN.
  • Empty slices return NaN values that preserve MATLAB-compatible shape semantics.
  • Dimensions larger than ndims(X) leave the input unchanged.

median Function GPU Execution Behaviour

When RunMat Accelerate is active, tensors that already live on the GPU remain on the device. The runtime asks providers for a dedicated median reduction. If the provider cannot supply one or cannot honour omitnan, RunMat gathers GPU data back to the host and executes the CPU path to preserve MATLAB semantics (including even-length averaging and ordering ties).

Examples of using the median function in MATLAB / RunMat

Finding the median of an odd-length vector

x = [7 2 9 4 5];
m = median(x);

Expected output:

m = 5;

Computing the median of an even-length vector with averaging

x = [1 4 9 10];
m = median(x);

Expected output:

m = 6.5;

Column-wise median of a matrix

A = [1 3 5; 7 9 11; 2 4 6];
colMedians = median(A);

Expected output:

colMedians = [2 4 6];

Row medians while ignoring NaN values

A = [1 NaN 3; 4 5 NaN];
rowMedians = median(A, 2, 'omitnan');

Expected output:

rowMedians = [2; 4.5];

Median of all elements in a matrix

A = reshape(1:6, [3 2]);
overall = median(A, 'all');

Expected output:

overall = 3.5;

Median of all elements on a GPU-backed tensor

G = gpuArray(randn(2048, 2048));
overall = median(G, 'omitnan');
result = gather(overall);

The planner keeps the tensor on the device for upstream work, gathers for the median, and returns the scalar result.

GPU residency in RunMat (Do I need gpuArray?)

You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).

In RunMat, the fusion planner keeps residency on GPU in branches of fused expressions. As such, in the above example, the result of the median call will already be on the GPU when the fusion planner has detected a net benefit to operating the fused expression it is part of on the GPU.

To preserve backwards compatibility with MathWorks MATLAB, and for when you want to explicitly bootstrap GPU residency, you can call gpuArray explicitly to move data to the GPU if you want to be explicit about the residency.

Since MathWorks MATLAB does not have a fusion planner, and they kept their parallel execution toolbox separate from the core language, as their toolbox is a separate commercial product, MathWorks MATLAB users need to call gpuArray to move data to the GPU manually whereas RunMat users can rely on the fusion planner to keep data on the GPU automatically.

FAQ

When should I use the median function?

Use median when you need a robust measure of central tendency that resists outliers better than the mean.

How are NaN values handled?

Use 'omitnan' to ignore NaN entries. The default 'includenan' propagates NaN if any element in the slice is NaN.

Does median convert logical values?

Yes. Logical arrays are converted to double precision before the median is taken, matching MATLAB.

What happens with even-length slices?

Even-length slices return the arithmetic mean of the two middle values after sorting, matching MATLAB behaviour.

Can I compute the median along a specific dimension?

Yes. Pass a dimension index (>= 1) as the second argument: median(A, 2) reduces across rows.

What if I ask for a dimension larger than ndims(A)?

RunMat treats trailing dimensions as having size 1, so the result is the original array unchanged.

Does median fully execute on the GPU today?

When the active provider implements the reduce_median* hooks (e.g., the WGPU backend), the reduction stays on the device. Otherwise the runtime gathers to the CPU to guarantee MATLAB-compatible semantics.

See Also

mean, sum, prod, gpuArray, gather

Source & Feedback