isfinite — Test finite values element-wise in MATLAB and RunMat.
mask = isfinite(x) returns a logical mask indicating which elements are finite (not NaN or ±Inf). Output shape and type handling follow MATLAB semantics.
Syntax
tf = isfinite(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input value to test for finiteness. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | Logical mask for finite elements. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:isfinite:InvalidInput | Input is not numeric, logical, char, or string. | isfinite: expected numeric, logical, char, or string input |
RunMat:isfinite:InternalError | Internal mask-construction or gather path fails. | isfinite: internal error |
How isfinite works
- Numeric scalars return a logical scalar (
true/false). - Numeric arrays return a logical array of the same size, with
truewherever the corresponding element is finite. - Complex inputs report
truewhen both the real and imaginary components are finite. - Logical inputs return
truebecause logical values (0 or 1) are finite by construction. - Character arrays return logical arrays of ones (characters map to finite Unicode code points).
- String scalars return
false; string arrays return logical arrays of zeros, mirroring MATLAB behavior. - When the input is a
gpuArray, RunMat keeps the computation on the device if the active acceleration provider implements thelogical_isfinitehook; otherwise the runtime gathers the data back to the host automatically.
Does RunMat run isfinite on the GPU?
When RunMat Accelerate is active, isfinite looks for the provider hook logical_isfinite. Providers that implement the hook execute the finite 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 finite
result = isfinite(42)Expected output:
result =
1Create a finite mask for a numeric matrix
A = [1 NaN; Inf 4];
mask = isfinite(A)Expected output:
mask =
2×2 logical array
1 0
0 1Identify finite components in a complex array
Z = [1+2i Inf+0i 3+NaNi];
mask = isfinite(Z)Expected output:
mask =
1×3 logical array
1 0 0Apply isfinite to character data
chars = ['R' 'u' 'n'];
mask = isfinite(chars)Expected output:
mask =
1×3 logical array
1 1 1Run isfinite directly on the GPU
G = gpuArray([1 -Inf 5]);
mask_gpu = isfinite(G);
mask = gather(mask_gpu)Expected output:
mask =
1×3 logical array
1 0 1Using isfinite with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how isfinite changes the result.
Run a small isfinite example, explain the result, then change one input and compare the output.
FAQ
Does isfinite treat NaN or Inf values as finite?⌄
No. isfinite only returns true for values that are neither NaN nor infinite. Use isnan or isinf if you need to distinguish between those cases.
What does isfinite return for logical inputs?⌄
Logical inputs always produce true because logical values are limited to 0 or 1, which are finite.
How does isfinite handle complex numbers?⌄
It returns true only when both the real and imaginary components of the element are finite, matching MATLAB semantics.
What happens with string or character inputs?⌄
String scalars return false, and string arrays return logical zeros. Character arrays return logical ones because their Unicode code points are finite.
Can I fuse isfinite with other elementwise operations?⌄
Yes. The fusion planner treats isfinite as an elementwise operation, so expressions like isfinite(A ./ B) remain eligible for GPU fusion when the provider advertises support.
Is there a performance difference between isfinite, isnan, and isinf?⌄
Each predicate performs a single elementwise test. Performance is dominated by memory bandwidth, so they have comparable cost on both CPU and GPU.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isfinite is executed, line by line, in Rust.
- View the source for isfinite 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.