RunMat
GitHub

and — Element-wise logical AND for scalars and arrays in MATLAB and RunMat.

and(A, B) (or A & B) computes the element-wise logical AND of its inputs. Nonzero values (including NaN) are treated as true, and scalar/singleton expansion follows MATLAB and RunMat broadcasting rules.

Syntax

tf = and(A, B)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesLeft operand.
BAnyYesRight operand.

Returns

NameTypeDescription
tfLogicalArrayLogical element-wise conjunction result.

Errors

IdentifierWhenMessage
RunMat:and:InvalidInputAn input is not logical, numeric, complex, character, or gpuArray with gatherable numeric data.and: unsupported input type; expected logical, numeric, complex, or character data
RunMat:and:SizeMismatchInput shapes are not broadcast-compatible.and: array sizes are not compatible for broadcasting

How and works

  • Accepts logical, numeric, complex, and character arrays; character code points of zero evaluate to false.
  • Supports MATLAB-style implicit expansion so scalars and singleton dimensions broadcast automatically.
  • Propagates empty dimensions: if a broadcasted axis has length 0, the output is an empty logical array with the same shape.
  • Treats NaN values as true, matching MATLAB's element-wise logical semantics.
  • Keeps gpuArray inputs on device when the active provider exposes the logical_and hook; otherwise the runtime gathers to host transparently.

Does RunMat run and on the GPU?

When both operands reside on the GPU and the active provider implements the logical_and hook, RunMat lowers the call into a device kernel that writes 0 or 1 for each element. The fusion planner treats and as an element-wise operation, so fused expressions (for example, and(A > 0, B)) stay on device without intermediate gathers. If the provider lacks the hook, the runtime gathers the inputs to host memory automatically and executes the CPU implementation instead of failing.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's native auto-offload logic moves data to the GPU when a fused expression benefits from device execution, and results stay resident until you gather them or the planner needs host access. Call gpuArray only when you want to seed residency manually, or when you are porting MATLAB code that already uses explicit device transfers.

Examples

Check if two logical scalars are both true

result = and(true, false)

Expected output:

result =
     0

Combine numeric arrays element-wise with logical AND

A = [1 0 2 0];
B = [3 4 0 0];
C = and(A, B)

Expected output:

C =
  1×4 logical array
     1     0     0     0

Apply logical AND with automatic scalar expansion

mask = [1; 0; 3; 0];
flag = and(mask, 5)

Expected output:

flag =
  4×1 logical array
     1
     0
     1
     0

Use logical AND with character arrays

lhs = ['R' 'u' 'n'];
rhs = ['R' 'u' 0];
match = and(lhs, rhs)

Expected output:

match =
  1×3 logical array
     1     1     0

Run logical AND directly on the GPU

G1 = gpuArray([0 2 0 4]);
G2 = gpuArray([1 0 3 4]);
deviceResult = and(G1, G2)
hostResult = gather(deviceResult)

Expected output:

deviceResult =
  1×4 gpuArray logical array
     0     0     0     1
hostResult =
  1×4 logical array
     0     0     0     1

Using and with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how and changes the result.

Run a small and example, explain the result, then change one input and compare the output.

FAQ

Does and return logical values?

Yes. The result is a logical scalar (true/false) when the broadcasted shape contains exactly one element; otherwise the function returns a logical array. On the GPU the kernel writes 0.0/1.0 elements, and the runtime converts them back to logical values when you gather.

How are NaN values handled?

NaN counts as true. For example, and(NaN, 5) returns true because both operands evaluate to non-zero.

Is implicit expansion supported?

Yes. The inputs follow MATLAB-style implicit expansion rules: dimensions of length 1 broadcast across the other input. Fully incompatible shapes raise a size-mismatch error.

Can I use and with complex numbers?

Yes. Real or complex inputs return true when either the real or imaginary component is non-zero. For example, and(1 + 0i, 0 + 2i) returns true.

How does and differ from the & operator?

They share the same element-wise semantics. The functional form is convenient for higher-order code (for example, arrayfun(@and, ...)) and for aligning with MATLAB documentation. Use && or || for short-circuit scalar logic.

What happens when only one input is a gpuArray?

RunMat promotes the other input to the GPU before dispatch when the auto-offload planner decides it is profitable. If the provider lacks a device implementation, both operands gather to host automatically and the logical result executes on the CPU.

Bit

not · or · xor

Rel

eq · ge · gt · isequal · le · lt · ne

Logical

logical

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how and is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.