eq — Compute element-wise equality in MATLAB and RunMat.
eq(A, B) (or A == B) compares inputs element-wise for equality. Scalar expansion and output-shape behavior follow MATLAB and RunMat logical comparison semantics.
Syntax
tf = eq(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 equality result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:eq:InvalidInput | Operands contain unsupported types or mixed numeric/string domains. | eq: mixing numeric and string inputs is not supported |
RunMat:eq:SizeMismatch | Operands are not broadcast-compatible. | eq: array sizes are not compatible for broadcasting |
How eq works
- Numeric, logical, and character inputs are compared element-wise using MATLAB's implicit expansion rules.
- Complex numbers are equal only when both their real and imaginary parts match.
- Character arrays compare by Unicode code point; you can mix them with numeric arrays (
'A' == 65) or strings. - String scalars and string arrays compare lexically; implicit expansion works across the string dimensions.
- Handle objects compare by identity rather than by structural equality.
- Mixed numeric/string inputs raise MATLAB-compatible type errors.
Does RunMat run eq on the GPU?
When both operands are gpuArray values and the active acceleration provider implements the elem_eq hook, RunMat executes the comparison entirely on the device and returns a gpuArray logical result. If the provider does not expose this hook, the runtime gathers the inputs to host memory automatically and performs the CPU comparison instead of failing.
GPU memory and residency
You usually do not need to call gpuArray explicitly. RunMat's native auto-offload planner keeps intermediate results on the GPU when fused expressions benefit from device execution. Explicit gpuArray and gather calls remain available for compatibility with MATLAB code that manages residency manually.
Examples
Check If Two Scalars Are Equal
flag = eq(42, 42)Expected output:
flag =
1Compare Two Vectors Element-Wise
A = [1 2 3 4];
B = [1 0 3 5];
mask = eq(A, B)Expected output:
mask =
1×4 logical array
1 0 1 0Apply Equality With Implicit Expansion
M = [1 2 3; 4 5 6];
sel = eq(M, 2)Expected output:
sel =
2×3 logical array
0 1 0
0 0 0Compare Character Data To Numeric Codes
letters = ['A' 'B' 'C'];
isA = eq(letters, 65)Expected output:
isA =
1×3 logical array
1 0 0Compare String Arrays To A Scalar String
names = ["alice" "bob" "alice"];
matches = eq(names, "alice")Expected output:
matches =
1×3 logical array
1 0 1Run eq Directly On gpuArray Inputs
G1 = gpuArray([1 2 3]);
G2 = gpuArray([1 0 3]);
deviceResult = eq(G1, G2)
hostResult = gather(deviceResult)Expected output:
deviceResult =
1 0 1
hostResult =
1 0 1Using eq with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how eq changes the result.
Run a small eq example, explain the result, then change one input and compare the output.
FAQ
Does eq return logical values?⌄
Yes. Scalars return true or false. Arrays return logical arrays, and gpuArray inputs return gpuArray logical outputs.
How are NaN values treated?⌄
NaN == NaN evaluates to false, matching MATLAB's behaviour.
Can I compare complex numbers with eq?⌄
Yes. Both the real and imaginary parts must match for the comparison to return true.
Are character vectors treated as numbers or text?⌄
Both: they compare numerically (character code) against numeric inputs, and textually when compared to strings or other character arrays.
What happens when I mix numeric and string inputs?⌄
RunMat raises a MATLAB-compatible error describing the unsupported type combination.
Do handle objects compare by value?⌄
No. Handles compare by identity: two handles are equal only when they reference the same underlying object.
Does implicit expansion apply to string arrays?⌄
Yes. String arrays support MATLAB-style implicit expansion, so you can compare against scalar strings without manual replication.
Can I chain eq inside fused GPU expressions?⌄
Yes. The builtin registers element-wise fusion metadata so the planner can fuse comparisons with surrounding GPU-friendly operations.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how eq is executed, line by line, in Rust.
- View the source for eq 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.