Array Comparison with Relational Operators
In this section...
“Array Comparison” on page 2-29
“Logic Statements” on page 2-31
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.
Symbol
Function Equivalent
Description
<
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” on page 2-25.
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 =
Array Comparison with Relational Operators
2-29
Précédent

- 79/1450

Suivant