~=

Not-equal operator: tests element by element whether two values differ.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

A ~= B

Returns a logical array that is true where A and B differ. Same as ne(A, B).

Description

The comparison is made element by element, so [1 2 3] ~= [1 5 3] gives 0 1 0. A scalar is compared with every element of an array. Character arrays are compared by character code, and a string scalar is compared with another string as a whole.

NaN ~= NaN is true, since NaN is never equal to anything. For complex numbers both the real and the imaginary parts are compared. To test whether two whole arrays differ, use ~isequal(A, B) instead, which returns one scalar.

Input arguments

A, B
Numeric, logical or character arrays, strings, or other comparable values. Sizes must be compatible, with implicit expansion of singleton dimensions.

Output arguments

TF
Logical array with the size of the expanded inputs.

Example

Element-wise inequality

[1 2 3] ~= [1 5 3]
x = 3;
x ~= [1 3 5]
NaN ~= NaN
"cat" ~= "car"
Output
ans = 1×3 logical array

   0   1   0
ans = 1×3 logical array

   1   0   1
ans = logical
   1
ans = logical
   1

See also

eq ne isequal lt gt