Relational Operations
A relational operation applies a relational operator or relational function to a pair of operands and returns a logical value.
- Relational operations are often combined with Matlab Logical Operations to form complex conditional expressions.
Element-Wise Relational Operations
| Operator | Equivalent Function |
|---|---|
== | eq |
~= | ne |
>= | ge |
> | gt |
<= | le |
< | le |
- All operands must be ordinary arrays—not cell or structure arrays
- Fully supported types:
single,double,int8,int16,int32,int64,uint8,uint16,uint32,uint64,logical,char,string,categorical,datetime,duration
- Fully supported types:
- Operand sizes must be compatible
- For arrays of the same size, comparison is element-wise at matching indices, returning a logical array of the same size
- When one operand is a scalar, it is compared against every element of the other
- For characters, the comparison is lexicographic
- For complex numbers
- The first two relational operations (
==,~=) compare both real and imaginary parts - The remaining four operations compare only the real part
- The first two relational operations (
- The constant
InfequalsInf - The constant
NaNis unequal to any value, including itself - For floating-point values, the storage format imposes a relative error, so it is best not to test equality of two floats directly. Instead, use one of the last four comparisons to bound the value within a tolerance interval that meets your needs
- For integer-valued data—whether of class
double, an integer class, logical, or character (code point)—comparisons can be made in the usual way
Whole-Array Relational Operations
The functions isequal and isequaln compare any two values and return a single logical value indicating whether they are identical.
- The exact definition of “identical” is given in Matlab Functions - isequal
isequalndiffers from isequal only in that indeterminate elements such asNaNare treated as equal to themselves