nan — Create arrays filled with IEEE NaN values using MATLAB-compatible size, class, and like forms.
nan(...) creates NaN-filled arrays across scalar, vector, matrix, and N-D size signatures, with MATLAB-compatible class and 'like' option behavior.
Syntax
A = nan(2, 3)How nan works
nan()returns the scalarNaN.nan(n)returns ann x ndouble array filled withNaN.nan(m, n, ...)returns a dense double array with the requested dimensions.nan(sz)accepts a size vector and returns an array withprod(sz)NaN elements.nan(A)returns a NaN array with the same size asA.nan(___, 'single')returns a single-precision numeric array.nan(___, 'like', prototype)matches numeric precision and device residency for supported prototype arrays.
Does RunMat run nan on the GPU?
Provider-backed nan construction uses constant-fill hooks, so fully resident GPU pipelines can create NaN arrays without first materializing a host tensor when provider support is available.
GPU memory and residency
When a GPU prototype is supplied, RunMat preserves residency by allocating the output through the active acceleration provider. If the provider cannot fill on device, RunMat falls back to a host NaN tensor and uploads it.
Examples
Creating a 2x3 matrix of NaN values
A = nan(2, 3)Expected output:
A = [NaN NaN NaN; NaN NaN NaN]Creating a single-precision NaN array
A = nan(4, 1, 'single')Expected output:
A = [NaN; NaN; NaN; NaN]Creating a GPU-resident NaN array like a prototype
G = gpuArray(rand(2,2));
A = nan(2, 2, 'like', G)Expected output:
A = [NaN NaN; NaN NaN]Using nan with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how nan changes the result.
Run a small nan example, explain the result, then change one input and compare the output.
FAQ
How is nan different from NaN?⌄
Both bare identifiers resolve to the scalar IEEE NaN constant. The callable form nan(...) constructs NaN-filled arrays.
Does nan(n) create a row vector?⌄
No. MATLAB compatibility requires nan(n) to create an n x n matrix. Use nan(1, n) or nan(n, 1) for vectors.
Can nan create GPU arrays?⌄
Yes. With a GPU prototype, nan(..., 'like', G) asks the active acceleration provider to allocate a NaN-filled GPU tensor.
Related Array functions
Creation
colon · eye · false · fill · inf · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how nan is executed, line by line, in Rust.
- View the source for nan 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.