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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar or array. |
B | Any | Yes | — | Second operand for element-wise maximum. |
placeholder | Any | No | [] | Empty placeholder selecting reduction-argument grammar. |
dim | Any | No | [] | Reduction dimension selector (scalar or dimension vector). |
flag | StringScalar | No | "all" | Reduction mode flag: "all" or "linear". |
optionName | StringScalar | No | "ComparisonMethod" | Option name (currently "ComparisonMethod"). |
method | StringScalar | No | "auto" | Comparison method: "auto", "abs"/"magnitude", or "real". |
Returns
| Name | Type | Description |
|---|---|---|
M | NumericArray | Maximum values. |
I | NumericArray | One-based maximum indices/origins. |
Returned values from nanmax depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:max:InvalidArgument | Argument grammar, dimensions, or option names/values are invalid. | max: invalid argument |
RunMat:max:InvalidInput | Input values cannot be converted to supported max domains. | max: invalid input |
RunMat:max:SizeMismatch | Element-wise operands are not broadcast-compatible. | max: size mismatch |
RunMat:max:Internal | Execution 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 skipsNaNvalues in each slice.nanmax(X, [], dim)andnanmax(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 asmaxwhile still omitting NaNs.nanmax(A, B)performs elementwise maximum with MATLAB implicit expansion and chooses the non-NaN side when exactly one operand isNaN.- If every value considered for a result is
NaN, the returned value isNaN. - 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.
Related Stats functions
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how nanmax is executed, line by line, in Rust.
- View the source for nanmax 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.