Main Content

Array Comparison with Relational Operators

Relational operators compare operands quantitatively, using operators like “less than”, “greater than”, and “not equal to.” The result of a relational comparison is a logical array indicating the locations where the relation is true.

These are the relational operators in MATLAB®.

SymbolFunction EquivalentDescription
<lt

Less than

<=le

Less than or equal to

>gt

Greater than

>=ge

Greater than or equal to

==eq

Equal to

~=ne

Not equal to

Array Comparison

Numeric Arrays

The relational operators perform element-wise comparisons between two arrays. The arrays must have compatible sizes to facilitate the operation. Arrays with compatible sizes are implicitly expanded to be the same size during execution of the calculation. In the simplest cases, the two operands are arrays of the same size, or one is a scalar. For more information, see Compatible Array Sizes for Basic Operations.

For example, if you compare two matrices of the same size, then the result is a logical matrix of the same size with elements indicating where the relation is true.

A = [2 4 6; 8 10 12]
A =

     2     4     6
     8    10    12
B = [5 5 5; 9 9 9]
B =

     5     5     5
     9     9     9
A < B
ans =

     1     1     0
     1     0     0

Similarly, you can compare one of the arrays to a scalar.

A > 7
ans =

     0     0     0
     1     1     1

If you compare a 1-by-N row vector to an M-by-1 column vector, then MATLAB expands each vector into an M-by-N matrix before performing the comparison. The resulting matrix contains the comparison result for each combination of elements in the vectors.

A = 1:3
A =

     1     2     3
B = [2; 3]
B =

     2
     3
A >= B
ans =

     0     1     1
     0     0     1

Empty Arrays

The relational operators work with arrays for which any dimension has size zero, as long as both arrays have compatible sizes. This means that if one array has a dimension size of zero, then the size of the corresponding dimension in the other array must be 1 or zero, and the size of that dimension in the output is zero.

A = ones(3,0);
B = ones(3,1);
A == B
ans =

   Empty matrix: 3-by-0

However, expressions such as

A == []

return an error if A is not 0-by-0 or 1-by-1. This behavior is consistent with that of all other binary operators, such as +, -, >, <, &, |, and so on.

To test for empty arrays, use isempty(A).

Complex Numbers

  • The operators >, <, >=, and <= use only the real part of the operands in performing comparisons.

  • The operators == and ~= test both real and imaginary parts of the operands.

Inf, NaN, NaT, and undefined Element Comparisons

  • Inf values are equal to other Inf values.

  • NaN values are not equal to any other numeric value, including other NaN values.

  • NaT values are not equal to any other datetime value, including other NaT values.

  • Undefined categorical elements are not equal to any other categorical value, including other undefined elements.

Logic Statements

Use relational operators in conjunction with the logical operators A & B (AND), A | B (OR), xor(A,B) (XOR), and ~A (NOT), to string together more complex logical statements.

For example, you can locate where negative elements occur in two arrays.

A = [2 -1; -3 10]
A =

     2    -1
    -3    10
B = [0 -2; -3 -1]
B =

     0    -2
    -3    -1
A<0 & B<0
ans =

     0     1
     1     0

For more examples, see Find Array Elements That Meet a Condition.

See Also

| | | | |

Related Topics