View all functions

CategoryLogical: Tests
GPUYes

What does the isfinite function do in MATLAB / RunMat?

mask = isfinite(x) returns a logical scalar or array indicating which elements of x are finite (not NaN, +Inf, or -Inf). The output matches MATLAB's semantics for scalars, matrices, higher-dimensional tensors, and gpuArray values.

How does the isfinite function behave in MATLAB / RunMat?

  • Numeric scalars return a logical scalar (true/false).
  • Numeric arrays return a logical array of the same size, with true wherever the corresponding element is finite.
  • Complex inputs report true when both the real and imaginary components are finite.
  • Logical inputs return true because 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 the logical_isfinite hook; otherwise the runtime gathers the data back to the host automatically.

Examples of using the isfinite function in MATLAB / RunMat

Check if a scalar is finite

result = isfinite(42);

Expected output:

result =
     1

Create 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     1

Identify 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     0

Apply isfinite to character data

chars = ['R' 'u' 'n'];
mask = isfinite(chars);

Expected output:

mask =
  1×3 logical array
     1     1     1

Run 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     1

isfinite Function GPU Execution Behaviour

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 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 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.

See Also

isinf, isnan, isreal, gpuArray, gather