Follow this blog to explore how relational operators work in MATLAB.
What are Relational Operators in MATLAB?
Relational operators are the building blocks of MATLAB and they are used for performing the comparison between two statements, expressions, or values. These operators perform the comparison between two values and provide logical values 1(True) or 0 (False).
These operators are listed below:
Operator | Functionality |
---|---|
== | Determines equality |
~= | Determines inequality |
< | Determines less than |
> | Determines greater than |
<= | Determines less than or equal to |
>= | Determines greater than or equal to |
Let’s explain the functionality of all these relational operators with examples.
1: Using Equal to Operator (==)
The relational operator equal to (==) determines the equality between two statements or expressions. This operator provides 1 if both values are equal otherwise, it provides 0.
Syntax
The (==) operator uses a simple syntax in MATLAB.
Here,
The given relation A==B determines the equality between two given values A and B.
Example
In this example, we use the equal to operator to compare two vectors A and B. Here, the equal to operator will return a logical array having the same size as the given arrays A and B. The logical array will contain the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A == B
2: Using Not Equal to Operator (~=)
The not equal to (~=) relational operator determines the inequality between two statements or expressions. This operator returns 1 if both values are not equal otherwise, it returns 0.
Syntax
The not equal to operator uses a simple syntax in MATLAB.
Here,
The given relation A~=B determines the inequality between two given values A and B.
Example
MATLAB code determines inequality between two vectors A and B using the not equal to operator. Here, the not equal to operator will return a logical array having the same size as the given arrays A and B. The logical array will have the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A ~= B
3: Using Less Than Operator (<)
The less than (<) relational operator determines which value is less than the other. If A and B are two values, it gives logical value 1 if A is less than B otherwise it gives 0.
Syntax
The less than operator uses a simple syntax in MATLAB.
Here,
The given relation A<B indicates the value of A is less than the value of B.
Example
The given example uses the less than operator to identify which values of vector A are less than the corresponding values of vector B. Here, the less than operator will return a logical array having the same size as the given arrays A and B. The logical array will contain the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A < B
4: Using Greater Than Operator (>)
The greater than (>) relational operator determines which value is greater than the other. If A and B are two values, it provides the logical value 1 if A is greater than B otherwise it provides 0.
Syntax
The greater than operator uses a simple syntax in MATLAB.
Here,
The given relation A>B shows the A’s value is greater than the B’s value.
Example
In this MATLAB code, we identify the values of vector A that are greater than the corresponding values of vector B. Here, the greater than operator will return a logical array having the same size as the given arrays A and B. The logical array will have the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A > B
5: Using Less or Equal to Operator (<=)
The less or equal to (<=) relational operator determines which value is less or equal to the other. If A and B are two values, it provides the logical value 1 if A is less or equal to B otherwise it provides 0.
Syntax
The less than or equal to operator implements a simple syntax in MATLAB.
Here,
The given relation A<=B identifies the value of A is less or equal to value B.
Example
This example uses the less or equal to operator to compare two vectors A and B. Here, the less than or equal to operator will return a logical array having the same size as the given arrays A and B. The logical array will contain the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A <= B
6: Using Greater or Equal to Operator (>=)
The greater or equal to (>=) operator identifies which value is greater or equal to the other. If A and B are two values, it provides logical value 1 if A is greater or equal to B otherwise it provides 0.
Syntax
The less than or equal to operator operates using the simple syntax in MATLAB.
Here,
The given relation A>=B determines the A’s value is greater or equal to B’s value.
Example
The given MATLAB code uses the greater than or equal to operator to compare two vectors A and B. Here, the greater than or equal to operator will return a logical array having the same size as the given arrays A and B. The logical array will contain the logical values 1 and 0 after comparing the elements of vector A with the corresponding elements of vector B.
B = [9 -3 2 10i];
A >= B
Conclusion
Like the other programming languages, MATLAB is also capable of supporting relational operators. These operators are utilized for performing comparisons between two expressions or statements. They include the equal to operator (==), not equal to operator (~=), less than operator (<), less than or equal to operator (<=), greater than operator (>), and greater than or equal to operator (>=). This tutorial has discovered the functionality of all these operators using examples.