RunMat
GitHub

not — Element-wise logical negation for scalars, arrays, and gpuArray values.

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 not works in 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 become false.
  • 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.
  • NaN evaluates to true, so not(NaN) produces false, consistent with MATLAB.

How not runs 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 =
     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

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.

These functions work well alongside not. Each page has runnable examples you can try in the browser.

and, or, xor, gpuArray, gather

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how not works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.