What does the not function do in MATLAB / RunMat?
not(X) inverts the logical interpretation of every element in X. Values that evaluate to true become false, and vice versa. MATLAB treats any non-zero (or non-empty) numeric or complex value as true, including NaN.
How does the not function behave in MATLAB / RunMat?
- Works on scalars, vectors, matrices, and N-D tensors with MATLAB broadcasting semantics.
- Accepts logical, numeric, complex, and character arrays. Character code points equal to zero become
true, all others becomefalse. - Returns a logical scalar when the input has exactly one element; otherwise the result is a logical array matching the input shape.
- Honors gpuArray residency. If the active acceleration provider exposes
logical_not, the entire operation runs on the GPU; otherwise RunMat falls back to the CPU path automatically. NaNevaluates totrue, sonot(NaN)producesfalse, consistent with MATLAB.
not Function GPU Execution Behaviour
When RunMat Accelerate is active, not dispatches to the provider hook logical_not. Providers write 0 or 1 into a device buffer, keeping the result resident on the GPU. If the provider does not implement the hook, RunMat gathers the input to host memory, executes the CPU implementation, and (if the caller passed a gpuArray) returns a logical array on the host so the call never fails.
Examples of using the not function in MATLAB / RunMat
Checking if a scalar value is zero
result = not(5)
Expected output:
result =
0
Negating a logical mask to find the complement
mask = [true false true];
inverseMask = not(mask)
Expected output:
inverseMask =
1×3 logical array
0 1 0
Turning nonzero numeric entries into false values
A = [0 1 2 0];
B = not(A)
Expected output:
B =
1×4 logical array
1 0 0 1
Flipping zero and nonzero character codes
chars = ['A' 0 'C'];
flags = not(chars)
Expected output:
flags =
1×3 logical array
0 1 0
Performing logical NOT directly on the GPU
G = gpuArray([0 4 0 9]);
deviceResult = not(G);
hostResult = gather(deviceResult)
Expected output:
deviceResult =
1×4 gpuArray logical array
1 0 1 0
hostResult =
1×4 logical array
1 0 1 0
GPU residency in RunMat (Do I need gpuArray?)
You usually do not have to call gpuArray manually. RunMat's auto-offload planner moves data to the GPU when a fused expression benefits from device execution. The not builtin preserves existing residency; results remain on the GPU until you gather them or another operation requires host access. Use gpuArray when porting MATLAB code that already does so explicitly or when you want to pin tensors to the GPU ahead of time.
FAQ
Does not return logical values?
Yes. Scalar inputs yield logical scalars (true/false). Array inputs produce logical arrays where each element is either 0 or 1.
How does not treat NaN or complex numbers?
NaN and complex numbers with any non-zero component evaluate as true, so not(NaN) and not(1+2i) return false.
Can I pass a gpuArray to not?
Absolutely. If the provider implements logical_not, the negation runs entirely on the GPU. Otherwise the runtime gathers to the host, performs the operation, and returns a logical array.
What happens with empty arrays?
Empty inputs produce empty logical outputs with matching shape, preserving MATLAB's empty propagation semantics.
Is there a difference between not(X) and ~X?
No. They share the same element-wise semantics. The functional form is convenient for higher-order APIs or when passing the operator as a handle.
Does not modify the input in place?
No. It returns a new logical value. When operating on gpuArrays, the provider writes into a fresh buffer so the original data remains unchanged.