or — Compute element-wise logical OR across scalar or array inputs with MATLAB-compatible expansion.
or(A, B) returns logical true where either operand is nonzero, using MATLAB-compatible implicit expansion and logical conversion rules.
Syntax
tf = or(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left operand. |
B | Any | Yes | — | Right operand. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | Logical element-wise disjunction result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:or:InvalidInput | An input is not logical, numeric, complex, character, or gpuArray with gatherable numeric data. | or: unsupported input type; expected logical, numeric, complex, or character data |
RunMat:or:SizeMismatch | Input shapes are not broadcast-compatible. | or: array sizes are not compatible for broadcasting |
How or works
- MATLAB-compatible mode accepts logical and real numeric arrays, including every integer class with exact zero/nonzero truth semantics. RunMat compatibility mode additionally accepts complex and character arrays.
- 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
NaNvalues astrue, matching MATLAB's element-wise logical semantics. - Uses a validated floating
logical_orhook when available. Native integer handles bypass floating hooks and gather exactly; automatic residency may return to host, while explicit gpuArray inputs restore a logical gpuArray result.
Does RunMat run or on the GPU?
When both operands reside on the GPU and the active provider implements the logical_or hook, RunMat lowers the call into a device kernel that writes 0 or 1 for each element. The fusion planner treats or as an element-wise operation, so fused expressions (for example, or(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 either logical scalar is true
result = or(true, false)Expected output:
result =
1Combine numeric arrays element-wise with logical OR
A = [1 0 2 0];
B = [3 4 0 0];
C = or(A, B)Expected output:
C =
1×4 logical array
1 1 1 0Apply logical OR with automatic scalar expansion
mask = [1; 0; 3; 0];
flag = or(mask, 0)Expected output:
flag =
4×1 logical array
1
0
1
0Use logical OR with character arrays
lhs = ['R' 'u' 0];
rhs = ['R' 0 'n'];
match = or(lhs, rhs)Expected output:
match =
1×3 logical array
1 1 1Run logical OR directly on the GPU
G1 = gpuArray([0 2 0 4]);
G2 = gpuArray([1 0 3 0]);
deviceResult = or(G1, G2)
hostResult = gather(deviceResult)Expected output:
deviceResult =
1×4 gpuArray logical array
1 1 1 1
hostResult =
1×4 logical array
1 1 1 1Using or with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how or changes the result.
Run a small or example, explain the result, then change one input and compare the output.
FAQ
Does or 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, or(NaN, 0) returns true because at least one operand evaluates 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 or with complex numbers?⌄
Only in RunMat compatibility mode. Complex input is a classified RunMat extension where either nonzero component is true; MATLAB-compatible mode rejects it before dispatch. Character input is gated the same way.
How does or differ from the | operator?⌄
They share the same element-wise semantics. The functional form is convenient for higher-order code (for example, arrayfun(@or, ...)) and for aligning with MATLAB documentation. Use && or || for short-circuit scalar logic.
What happens when only one input is a gpuArray?⌄
The runtime may use provider execution when compatible. Otherwise it gathers transparently and computes exactly on the host; explicit gpuArray intent restores the logical result to the owning provider, while automatic residency may remain on host.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how or is executed, line by line, in Rust.
- View the source for or 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.