ne — Compute element-wise inequality comparisons in MATLAB and RunMat.
ne(A, B) (or A ~= B) compares inputs element-wise for inequality. Scalar expansion and output-shape behavior follow MATLAB semantics.
Syntax
tf = ne(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 inequality result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:ne:InvalidInput | Operands contain unsupported types or mixed numeric/string domains. | ne: mixing numeric and string inputs is not supported |
RunMat:ne:SizeMismatch | Operands are not broadcast-compatible. | ne: array sizes are not compatible for broadcasting |
How ne works
- Numeric, logical, and character inputs are compared element-wise using MATLAB's implicit expansion rules.
- Complex numbers are considered different when either their real or imaginary part differs.
- 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 ne on the GPU?
When both operands are gpuArray values and the active acceleration provider implements the elem_ne 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
Checking if two scalars differ
flag = ne(42, 7)Expected output:
flag =
1Finding mismatched elements between vectors
A = [1 2 3 4];
B = [1 0 3 5];
mask = ne(A, B)Expected output:
mask =
1×4 logical array
0 1 0 1Using implicit expansion for inequality
M = [1 2 3; 4 5 6];
sel = ne(M, 2)Expected output:
sel =
2×3 logical array
1 0 1
1 1 1Comparing text values to numeric codes
letters = ['A' 'B' 'C'];
notA = ne(letters, 65)Expected output:
notA =
1×3 logical array
0 1 1Running ~= directly on gpuArray inputs
G1 = gpuArray([1 2 3]);
G2 = gpuArray([0 2 4]);
deviceResult = ne(G1, G2);
hostResult = gather(deviceResult)Expected output:
deviceResult =
1×3 gpuArray logical array
1 0 1
hostResult =
1×3 logical array
1 0 1Using ne with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ne changes the result.
Run a small ne example, explain the result, then change one input and compare the output.
FAQ
Does ne 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 true, matching MATLAB's behaviour because equality comparisons return false.
Can I compare complex numbers with ne?⌄
Yes. Results are true when either the real or imaginary component differs.
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 different unless 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 ne inside fused GPU expressions?⌄
Yes. The builtin registers element-wise fusion metadata so the planner can fuse inequality checks with surrounding GPU-friendly operations.
Is there a shorthand for calling ne?⌄
Yes. You can use the operator form A ~= B, which maps directly to this builtin.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ne is executed, line by line, in Rust.
- View the source for ne 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.