View all functions

CategoryMath: Reduction
GPUYes

What does the min function do in MATLAB / RunMat?

min finds the smallest values in its input while preserving MATLAB semantics for reductions, elementwise comparisons, NaN handling, complex magnitude comparisons, and linear indexing.

How does the min function behave in MATLAB / RunMat?

  • min(X) on an m × n array reduces along the first non-singleton dimension, returning a row vector of column-wise minima and the corresponding indices (when requested).
  • min(X, [], dim) reduces along the specified dimension; min(X, [], vecdim) reduces along each dimension listed in vecdim.
  • min(X, [], 'all') collapses every element into a scalar and returns the linear index when two outputs are requested.
  • min(X, [], 'linear') is equivalent to 'all' but guarantees that the matching index is linear over X(:).
  • min(X, [], ..., 'omitnan') ignores NaN values inside each slice. If every element in a slice is NaN, the result for that slice is NaN and the index is NaN.
  • min(X, [], ..., 'includenan') (default) propagates NaN whenever a slice contains any NaN element, returning the index of the first NaN.
  • min(A, B) performs elementwise comparison using MATLAB's implicit expansion rules. The second output indicates whether the minimum came from A (index 1) or B (index 2).
  • Complex inputs follow MATLAB ordering: 'ComparisonMethod','auto' (default) compares magnitudes and breaks ties using phase angles, while 'real' compares real components first. 'abs' is an explicit alias for magnitude ordering on real and complex inputs.

min Function GPU Execution Behaviour

When RunMat Accelerate is active, tensors that already reside on the GPU stay on the device whenever the provider exposes reduce_min_dim (for dimension reductions) or reduce_min (for whole-array reductions). Requests that require omitnan, custom comparison modes, 'linear' indices, or complex arithmetic gather the data to the host, compute the MATLAB-compatible result, and return the output on the host. Elementwise min(A, B) currently executes on the host; the planner rematerializes tensors on the GPU when follow-on fused kernels make it profitable.

Examples of using the min function in MATLAB / RunMat

Finding column-wise minima of a matrix

A = [3 1 5; 4 2 6];
[m, idx] = min(A);

Expected output:

m   = [3 1 5];
idx = [1 2 1];

Reducing along the second dimension

A = [3 1 5; 4 2 6];
[m, idx] = min(A, [], 2);

Expected output:

m   = [1; 2];
idx = [2; 2];

Collapsing all elements with linear indices

A = reshape(1:12, [3 4]);
[m, idx] = min(A, [], 'all');

Expected output:

m   = 1;
idx = 1;  % linear index into A(:)

Ignoring NaN values during reduction

values = [NaN 4 2; 3 NaN 1];
[m, idx] = min(values, [], 1, 'omitnan');

Expected output:

m   = [3 4 1];
idx = [2 1 2];

Elementwise minimum with broadcasting

A = [1 4 7];
B = [2; 3; 5];
[C, origin] = min(A, B);

Expected output:

C =
     1     3     5
origin =
     1     2     2

Comparing complex values by magnitude

Z = [1+2i, 2+1i, -2+2i];
M = min(Z);                         % magnitude ordering
R = min(Z, [], 'ComparisonMethod', 'real');

Expected output:

M = 2.0000 + 1.0000i
R = 1.0000 + 2.0000i

GPU residency in RunMat (Do I need gpuArray?)

You typically do not need to call gpuArray manually. The fusion planner keeps tensors on the GPU between compatible kernels. When a reduction is supported by the active provider, the minimum values and indices stay on device. If a provider lacks the necessary hook, RunMat gathers data to the host, computes the result, and returns host tensors—subsequent fused GPU kernels can re-upload data when profitable.

FAQ

Can I request the linear index of the global minimum?

Yes. Use either min(X, [], 'all') or min(X, [], 'linear'). Both return a scalar minimum and the linear index into X(:) when you request two outputs.

Does min support 'ComparisonMethod' for real and complex arrays?

Absolutely. 'auto' or 'abs' compare magnitudes; 'real' compares the real component first. The returned values always match MATLAB, including tie-breaking rules.

What happens when all elements are NaN and 'omitnan' is requested?

The value result is NaN and the index is NaN, matching MATLAB's behavior for empty slices.

Can I mix elementwise comparisons with dimension reductions?

No. min(A, B) performs elementwise comparisons only. Use min(A, [], dim) when you want reductions along specific dimensions.

Do GPU reductions support 'omitnan' or custom comparison methods?

Not yet. Those requests fall back to the host implementation, which still honors MATLAB semantics. The output remains a host tensor in that case.

Are logical and integer inputs supported?

Yes. Logical arrays are promoted to double precision, and integer inputs are converted to double before comparison, matching MATLAB's numeric tower.

See Also

max, sum, mean, gpuArray, gather

Source & Feedback