nanmax — Return maximum values while omitting NaNs.

nanmax is a compatibility form for maximum reductions and pairwise comparisons that ignore NaN values. It maps to max with "omitnan" semantics.

Syntax

M = nanmax(A)
[M, I] = nanmax(A)
M = nanmax(A, B)
[M, I] = nanmax(A, B)
M = nanmax(A, [], dim)
[M, I] = nanmax(A, [], dim)
All supported nanmax forms
M = nanmax(A)
[M, I] = nanmax(A)
M = nanmax(A, B)
[M, I] = nanmax(A, B)
M = nanmax(A, [], dim)
[M, I] = nanmax(A, [], dim)
M = nanmax(A, [], vecdim)
[M, I] = nanmax(A, [], vecdim)
M = nanmax(A, [], "all")
[M, I] = nanmax(A, [], "all")
M = nanmax(A, [], "linear")
[M, I] = nanmax(A, [], "linear")
M = nanmax(A, [], "ComparisonMethod", method)
[M, I] = nanmax(A, [], "ComparisonMethod", method)
M = nanmax(A, B, "ComparisonMethod", method)
[M, I] = nanmax(A, B, "ComparisonMethod", method)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesInput scalar or array.
BAnyYesSecond operand for element-wise maximum.
placeholderAnyNo[]Empty placeholder selecting reduction-argument grammar.
dimAnyNo[]Reduction dimension selector (scalar or dimension vector).
flagStringScalarNo"all"Reduction mode flag: "all" or "linear".
optionNameStringScalarNo"ComparisonMethod"Option name (currently "ComparisonMethod").
methodStringScalarNo"auto"Comparison method: "auto", "abs"/"magnitude", or "real".

Returns

NameTypeDescription
MNumericArrayMaximum values.
INumericArrayOne-based maximum indices/origins.

Returned values from nanmax depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:max:InvalidArgumentArgument grammar, dimensions, or option names/values are invalid.max: invalid argument
RunMat:max:InvalidInputInput values cannot be converted to supported max domains.max: invalid input
RunMat:max:SizeMismatchElement-wise operands are not broadcast-compatible.max: size mismatch
RunMat:max:InternalExecution fails due to gather, provider, allocation, or conversion internals.max: internal failure

How nanmax works

  • nanmax(X) reduces along the first non-singleton dimension and skips NaN values in each slice.
  • nanmax(X, [], dim) and nanmax(X, [], vecdim) reduce along the specified dimension or dimension vector.
  • nanmax(X, [], 'all') collapses every element into a scalar; nanmax(X, [], 'linear') also returns linear indices when two outputs are requested.
  • nanmax(..., 'ComparisonMethod', method) supports the same real and complex comparison modes as max while still omitting NaNs.
  • nanmax(A, B) performs elementwise maximum with MATLAB implicit expansion and chooses the non-NaN side when exactly one operand is NaN.
  • If every value considered for a result is NaN, the returned value is NaN.
  • Two-output calls return one-based indices or origins using the same conventions as max.

GPU memory and residency

nanmax gathers GPU inputs to host memory when omit-NaN semantics are required, then returns the MATLAB-compatible host result.

Examples

Column-wise maxima ignoring NaNs

A = [NaN 4 2; 3 NaN NaN];
M = nanmax(A)

Expected output:

M = [3 4 2]

Pairwise maximum ignoring NaNs

P = nanmax([NaN 2 NaN], [3 NaN NaN])

Expected output:

P = [3 2 NaN]

Using nanmax with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how nanmax changes the result.

Run a small nanmax example, explain the result, then change one input and compare the output.

FAQ

Should new code use nanmax or max(..., 'omitnan')?

Use max(..., 'omitnan') for new MATLAB-style code. nanmax is provided for compatibility with existing codebases.

Does nanmax support pairwise array inputs?

Yes. nanmax(A, B) uses the same implicit expansion and origin-index behavior as max(A, B), while omitting NaNs.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how nanmax is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.