~

Logical NOT operator.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

B = ~A

Returns 1 where A is zero and 0 where it is nonzero. Same as not(A).

Description

~ inverts a mask: if m = x > 3 marks the large values, x(~m) selects the others. It binds more tightly than comparison and arithmetic operators, so ~x == y means (~x) == y, which is rarely what is wanted; write ~(x == y) or use x ~= y.

The same symbol, placed in an output list, as in [~, i] = max(x), tells MATLAB to ignore that output.

Input arguments

A
Numeric or logical array.

Output arguments

B
Logical array of the same size as A.

Example

x = [1 5 3 8];
m = x > 3
~m
x(~m)
[~, i] = max(x)
Output
m = 1×4 logical array

   0   1   0   1
ans = 1×4 logical array

   1   0   1   0
ans = 1×2

     1     3
i = 4

See also

not & | xor ~=