What does the ne function do in MATLAB / RunMat?
ne(A, B) (or the infix A ~= B) performs an element-wise inequality comparison. The result is a logical scalar when the broadcasted shape contains one element, or a logical array otherwise.
How does the ne function behave in MATLAB / RunMat?
- 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.
ne Function GPU Execution Behaviour
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.
Examples of using the ne function in MATLAB / RunMat
Checking if two scalars differ
flag = ne(42, 7);
Expected output:
flag =
1
Finding 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 1
Using 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 1
Comparing text values to numeric codes
letters = ['A' 'B' 'C'];
notA = ne(letters, 65);
Expected output:
notA =
1×3 logical array
0 1 1
Running ~= 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 1
GPU residency in RunMat (Do I need gpuArray?)
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.
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.