isnan — Return a logical mask indicating which elements of the input are NaN.
mask = isnan(x) returns a logical scalar or array indicating which elements of x are IEEE NaN (Not-a-Number) values. The output matches MATLAB's semantics for scalars, matrices, higher-dimensional tensors, and gpuArray values.
How isnan works in RunMat
- Numeric scalars return a logical scalar (
true/false). - Numeric arrays return a logical array of the same size, with
truewherever the corresponding element isNaN. - Complex inputs report
truewhen either the real or imaginary component isNaN. - Logical inputs return
falsebecause logical values are finite by definition. - Character arrays return logical arrays of zeros (characters map to finite code points).
- String arrays return logical arrays of zeros, mirroring MATLAB's behavior.
stringscalars (string objects) return a logical scalarfalse.- When the input is a
gpuArray, RunMat keeps the computation on the device if the active acceleration provider implements thelogical_isnanhook; otherwise the runtime gathers the data back to the host automatically.
How isnan runs on the GPU
When RunMat Accelerate is active, isnan looks for the provider hook logical_isnan. Providers that implement the hook execute the NaN test entirely on the GPU, producing a logical gpuArray result without any host transfers. If the hook is absent, RunMat gathers the input tensor back to the CPU, computes the mask on the host, and returns a regular logical array so the builtin always succeeds.
GPU memory and residency
You usually do **not** need to call gpuArray explicitly. RunMat's auto-offload planner keeps tensors on the GPU across fused expressions when that improves performance. You can still seed residency manually with gpuArray for compatibility with MATLAB scripts or when you want fine-grained control over data movement.
Examples
Check if a scalar is NaN
result = isnan(NaN)Expected output:
result =
1Create a NaN mask for a numeric matrix
A = [1 NaN 2; 3 4 NaN];
mask = isnan(A)Expected output:
mask =
2×3 logical array
0 1 0
0 0 1Identify NaNs inside a complex array
Z = [1+2i NaN+0i 3+NaNi];
mask = isnan(Z)Expected output:
mask =
1×3 logical array
0 1 1Use isnan with character data
chars = ['R' 'u' 'n'];
mask = isnan(chars)Expected output:
mask =
1×3 logical array
0 0 0Run isnan directly on the GPU
G = gpuArray([1 0/0 3]);
mask_gpu = isnan(G);
mask = gather(mask_gpu)Expected output:
mask =
1×3 logical array
0 1 0FAQ
Does isnan modify the input array?
No. It returns a logical mask and leaves the input unchanged, whether the data lives on the host or the GPU.
How does isnan treat complex numbers?
It returns true when either the real or the imaginary component of the element is NaN, matching MATLAB semantics.
What does isnan return for logical inputs?
Logical inputs always produce false because logical values (0 or 1) are finite.
Does isnan support string or character arrays?
Yes. Character arrays return logical zeros with the same shape. String arrays return logical zeros per element.
What happens when isnan runs on a gpuArray without provider support?
RunMat gathers the data to the host, computes the mask on the CPU, and returns a host logical array. This guarantees that the builtin never fails even when the GPU backend lacks the specialised kernel.
Can I fuse isnan with other elementwise operations?
Yes. The fusion planner treats isnan as an elementwise operation, so expressions like isnan(A ./ B) remain eligible for GPU fusion when the provider advertises support.
Are there performance differences between isnan and isfinite/isinf?
Each predicate performs a single elementwise test. Performance is dominated by memory bandwidth; all three functions have comparable cost on both CPU and GPU.
How does isnan behave on empty arrays?
It returns an empty logical array with the same size metadata as the input, matching MATLAB behavior.
What does isnan do in MATLAB?
isnan(A) returns a logical array the same size as A, with true where elements are NaN and false elsewhere. It works on real and complex arrays.
How do I remove NaN values from an array in MATLAB?
Use A(isnan(A)) = [] to delete NaN elements from a vector, or A(~isnan(A)) to select only non-NaN values. For matrices, use any(isnan(A), 2) to find rows containing NaN.
Does isnan support GPU acceleration in RunMat?
Yes. isnan runs on the GPU with elementwise fusion, meaning it can be combined with other operations without extra memory round-trips.
Related functions to explore
These functions work well alongside isnan. Each page has runnable examples you can try in the browser.
isfinite, isinf, isreal, gpuArray, gather, isgpuarray, islogical, isnumeric
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isnan works, line by line, in Rust.
- View isnan.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.