What does the isnan function do in MATLAB / RunMat?
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 does the isnan function behave in MATLAB / 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.
Examples of using the isnan function in MATLAB / RunMat
Check if a scalar is NaN
result = isnan(NaN);
Expected output:
result =
1
Create 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 1
Identify NaNs inside a complex array
Z = [1+2i NaN+0i 3+NaNi];
mask = isnan(Z);
Expected output:
mask =
1×3 logical array
0 1 1
Use isnan with character data
chars = ['R' 'u' 'n'];
mask = isnan(chars);
Expected output:
mask =
1×3 logical array
0 0 0
Run 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 0
isnan Function GPU Execution Behaviour
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 residency in RunMat (Do I need gpuArray?)
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.
FAQ
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.