Programming and Data Types | ![]() ![]() |
MATLAB provides these relational operators.
Operator |
Description |
< |
Less than |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
== |
Equal to |
~= |
Not equal to |
Relational Operators and Arrays
MATLAB's relational operators compare corresponding elements of arrays with equal dimensions. Relational operators always operate element-by-element. In this example, the resulting matrix shows where an element of A
is equal to the corresponding element of B
.
A = [2 7 6;9 0 5;3 0.5 6]; B = [8 7 0;3 2 5;4 -1 7]; A == B ans = 0 1 0 0 0 1 0 0 0
For vectors and rectangular arrays, both operands must be the same size unless one is a scalar. For the case where one operand is a scalar and the other is not, MATLAB tests the scalar against every element of the other operand. Locations where the specified relation is true receive the value 1
. Locations where the relation is false receive the value 0
.
Relational Operators and Empty Arrays
The relational operators work with arrays for which any dimension has size zero, as long as both arrays are the same size or one is a scalar. However, expressions such as
A == []
return an error if A is not 0-by-0 or 1-by-1.
To test for empty arrays, use the function
isempty(A)
![]() | Arithmetic Operators | Logical Operators | ![]() |