View all functions

CategoryMath: Reduction
GPUYes

What does the any function do in MATLAB / RunMat?

any(X) returns logical true wherever at least one element of the requested slice of X is nonzero. When you omit the dimension, the reduction runs along the first non-singleton axis, mirroring MATLAB.

How does the any function behave in MATLAB / RunMat?

  • Works with logical, numeric, complex, and character arrays; other types raise a descriptive error.
  • Accepts any(X, dim) to reduce along a single dimension or any(X, vecdim) to collapse multiple axes at once.
  • any(X, 'all') flattens the entire array into a single logical scalar.
  • any(___, 'omitnan') ignores NaN values (including complex parts) when deciding whether a slice contains nonzero content.
  • any(___, 'includenan') (default) treats NaN as logical true, matching MATLAB behaviour.
  • Empty dimensions yield logical zeros with MATLAB-compatible shapes; empty arrays reduced with 'all' return false.
  • Results are always host-resident logical scalars or logical arrays, even when the input tensor lives on the GPU, because the runtime copies the compact output back to the CPU.

any Function GPU Execution Behaviour

RunMat Accelerate keeps inputs resident on the GPU whenever possible. Providers that expose reduce_any_dim (and optionally reduce_any) perform the OR-reduction on device buffers, and the runtime then downloads the tiny logical result back to the CPU (every any call returns a host logical array). When those hooks are missing, RunMat gathers the input tensor and evaluates the reduction on the host instead, preserving MATLAB behaviour in all cases.

Examples of using the any function in MATLAB / RunMat

Checking if any column in a matrix is nonzero

A = [0 2 0; 0 0 0];
colHasData = any(A);

Expected output:

colHasData = [0 1 0];

Detecting whether any row contains a nonzero entry

B = [0 4 0; 1 0 0; 0 0 0];
rowHasData = any(B, 2);

Expected output:

rowHasData = [1; 1; 0];

Reducing across multiple dimensions with vecdim

C = reshape(1:24, [3 4 2]);
hasValues = any(C > 20, [1 2]);

Expected output:

hasValues = [0 1];

Checking all elements with the 'all' option

D = [0 0; 0 5];
anyNonZero = any(D, 'all');

Expected output:

anyNonZero = true;

Ignoring NaN values when probing slices

E = [NaN 0 0; 0 0 0];
withNaN = any(E);              % returns [1 0 0]
ignoringNaN = any(E, 'omitnan'); % returns [0 0 0]

Expected output:

withNaN = [1 0 0];
ignoringNaN = [0 0 0];

Running any on GPU arrays with automatic fallback

G = gpuArray([0 1 0; 0 0 0]);
gpuResult = any(G, 2);
hostResult = gather(gpuResult);

Expected output:

hostResult = [1; 0];

Evaluating any on character data

chars = ['a' 0 'c'];
hasPrintable = any(chars);

Expected output:

hasPrintable = [1 0 1];

GPU residency in RunMat (Do I need gpuArray?)

You usually do not need to call gpuArray manually. The fusion planner keeps GPU-resident inputs on the device and only gathers the small logical results that any produces. If your workload already uses explicit gpuArray/gather calls for MATLAB compatibility, RunMat honours them and still produces correct logical outputs.

FAQ

When should I use the any function?

Use any whenever you need to know if any element of an array, row, column, or sub-array is nonzero or logical true.

Does any always return logical values?

Yes. Results are logical scalars or logical arrays even when the computation involves GPU inputs.

How do I test a specific dimension?

Pass the dimension as the second argument (for example, any(X, 2) reduces each row). Provide a vector such as [1 3] to collapse multiple axes.

What does any(X, 'all') compute?

It reduces across every dimension of X and returns a single logical scalar indicating whether any element of the entire array is nonzero.

How are NaN values handled?

By default they count as nonzero ('includenan'). Add 'omitnan' to ignore them; if every element in a slice is NaN, the result becomes false.

Does any work with complex numbers?

Yes. Complex values are considered nonzero when either the real or imaginary component is nonzero. Complex values containing NaN obey the 'omitnan'/'includenan' rules.

Can I apply any to character arrays?

Yes. Characters compare against their Unicode code points; zero-valued code points are treated as false, and everything else is true.

What happens with empty inputs?

Empty reductions follow MATLAB semantics: dimensions of length zero produce logical zeros, while any(X, 'all') over an empty array evaluates to false.

How do GPU backends accelerate any?

Providers may expose specialised OR-reduction kernels (reduce_any_dim, reduce_any) or use fused_reduction to remain on the device. When such hooks are absent, RunMat downloads the small output and computes on the host.

See Also

sum, prod, mean, gpuArray, gather

Source & Feedback