not — Compute element-wise logical negation in MATLAB and RunMat.
not(X) inverts the logical interpretation of each element in X. Truthiness conversion and output shape follow MATLAB semantics.
Syntax
tf = not(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input operand. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | Logical element-wise negation result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:not:InvalidInput | The input is not logical, numeric, complex, character, or gpuArray with gatherable numeric data. | not: unsupported input type; expected logical, numeric, complex, or character data |
How not works
- 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.
Does RunMat run not on the GPU?
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.
GPU memory and residency
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.
Examples
Checking if a scalar value is zero
result = not(5)Expected output:
result =
0Negating a logical mask to find the complement
mask = [true false true];
inverseMask = not(mask)Expected output:
inverseMask =
1×3 logical array
0 1 0Turning nonzero numeric entries into false values
A = [0 1 2 0];
B = not(A)Expected output:
B =
1×4 logical array
1 0 0 1Flipping zero and nonzero character codes
chars = ['A' 0 'C'];
flags = not(chars)Expected output:
flags =
1×3 logical array
0 1 0Performing 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 0Using not with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how not changes the result.
Run a small not example, explain the result, then change one input and compare the output.
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.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how not is executed, line by line, in Rust.
- View the source for not 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.