Operators are special keywords or characters that perform specific functions. MATLAB supports the use of operators on both scalar and non-scalar data. The relational operators supported in MATLAB are listed and explained below:
Symbol | Description |
| Equal to |
| Not Equal to |
| Less than |
| Less than or equal to |
| Greater than |
| Greater than or equal to |
==
)The ==
operator is used to determine if two numbers or arrays are equal.
In the case of numbers, the answer is 1
if the values are equal, and 0
if the values are not equal.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the elements are equal, and 0
if the elements are not equal.
Let’s run some examples to understand this concept:
#Example 1X = 5Y = 5X == Y#Example 2X = 5Y = 3X == Y
In the example above, we compared two integer
variables X
and Y
. For example one value of both X
and Y
is 5
. Hence, the output of the ==
operator is 1
which also represents true
. Contrarily, in example two, the output is 0
, which is equivalent to the False
status.
X = [5 6 7 9]Y = [5 7 6 9]X == Y
In the example above, we compared two arrays, X
and Y
. The ==
operator matches each index of array X
with the corresponding index of array Y
(index 1 of X
with index 1 of Y
and so on, convention followed by each operator). We will get an output in form of an array giving 1
on an index where both arrays have the same value and 0
where it is different.
Note: In Matlab index of the array starts with
1
instead of0
.
X = [5 6 7]Y = 5X == Y
In the example above, we compared an array X
to a scaler Y
. The ==
operator matches each index of array X
with the variable Y
. Again, output will be in form of an array giving 1
on an index where each index of X
matches the value of variable Y
.
~=
)The ~=
operator is used to determine the inequality of two numbers or arrays.
In the case of numbers, the answer is 1
if the values are not equal, and 0
if they are equal.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the elements are not equal, and 0
if the elements are equal.
Let’s see some executable examples of this concept below:
# Comparing scalar to scalarX = 5Y = 5X ~= Y# we will get answer 0# Comparing Array to ArrayX = [5 6 7 9]Y = [5 7 6 9]X ~= Y# we will get answer [0 1 1 0]# Comparing Array to ScalarX = [5 6 7]Y = 5X ~= Y# we will get answer [0 1 1]
In the above examples, the ~=
operator outputs the exact opposite of what we saw in previous ==
operator examples.
>
)The >
operator is used to determine the greater of two numbers or arrays.
In the case of numbers, the answer is 1
if the value on the left side of >
is greater than the value on the right side. Otherwise, the answer is 0
.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the right array elements are greater than the left array elements. Otherwise, the answer array entries are 0
.
Let’s solve some examples:
# Comparing scalar to scalarX = 6Y = 5X > Y# we will get answer = 1# Comparing Array to ArrayX = [5 6 7 9]Y = [5 7 6 10]X > Y# we will get answer [0 0 1 0]# Comparing Array to ScalarX = [5 6 7]Y = 5X > Y# we will get answer [0 1 1]
>=
)The >=
operator is used to determine the greater of two numbers or arrays or if they are equal.
In the case of numbers, the answer is 1
if the value on the left side of >=
is greater than or equal to the value on the right side. Otherwise, the answer is 0
.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the right array elements are greater or equal to the corresponding left array elements. Otherwise, the answer array elements are 0
.
Let’s look into some examples:
# Comparing scalar to scalarX = 6Y = 5X >= Y# we will get ans = 1 as X is greater than Y.# Comparing Array to ArrayX = [5 6 7 9]Y = [5 7 6 10]X >= Y# we will get answer [1 0 1 0]# Comparing Array to ScalarX = [5 6 7]Y = 5X >= Y# we will get answer [1 1 1].
<
)The <
operator is used to determine the lesser of two numbers or arrays.
In the case of numbers, the answer is 1
if the value on the left side of <
is less than the value on the right side. Otherwise, the answer is 0
.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the right array elements are less than the corresponding left array elements. Otherwise, the answer array elements are 0
.
# Comparing scalar to scalarX = 6Y = 5X < Y# we will get answer 0# Comparing Array to ArrayX = [5 6 7 9]Y = [5 7 6 10]X < Y# we will get answer [0 1 0 1]# Comparing Array to ScalarX = [5 2 3]Y = 5X < Y# we will get answer [0 1 1]
<=
)The <=
operator is used to determine the lesser of two numbers or arrays or determine if they are equal.
In the case of numbers, the answer is 1
if the value on the left side of <=
is less than or equal to the value on the right side. Otherwise, the answer is 0
.
In the case of an array, an answer array is returned. The corresponding entries are 1
if the right array elements are less than or equal to the corresponding left array element. Otherwise, the answer array elements are 0
.
# Comparing scalar to scalarX = 6Y = 5X <= Y# we will get answer 0# Comparing Array to ArrayX = [5 6 7 9]Y = [5 7 6 10]X <= Y# we will get answer [1 1 0 1]# Comparing Array to ScalarX = [5 6 7]Y = 5X <= Y# we will get answer [1 0 0].
Free Resources