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.
Syntax
tf = isnan(A)Ais a numeric scalar or array (real, complex, orgpuArray). Logical, character, and string inputs are accepted and map tofalse, matching MATLAB.- For complex inputs,
isnanreturnstruewhenever either the real or imaginary component isNaN. tfis a logical array the same size asA, withtruewhere the corresponding element isNaNandfalseotherwise.
How isnan works
- 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 RunMat runs isnan 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.
What's the difference between isnan, isinf, and isfinite?⌄
isnan(x) is true only for NaN values, isinf(x) is true only for Inf and -Inf, and isfinite(x) is true for everything else (neither NaN nor infinite). For any numeric x, exactly one of isnan(x), isinf(x), or isfinite(x) is true.
How do I check for NaN without calling isnan?⌄
NaN is the only IEEE value that is not equal to itself, so x ~= x is true exactly when x is NaN. This trick works on scalars and arrays, but isnan is clearer, matches MATLAB idioms, and is what you should reach for in production code.
How do I filter NaN values out of a vector?⌄
Index with the negated mask: A(~isnan(A)) keeps only the finite entries. For matrices, use row-wise logic such as A(~any(isnan(A), 2), :) to keep rows with no NaN.
Related Logical functions
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.