Related Tags

matlab
communitycreator
relationaloperator

# What are relational operators in MATLAB? Zain Ali Babar

### Overview

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:

Relational operators
 Symbol Description == Equal to ~= Not Equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to

### 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.

//Comparing scalar to scalar
X = 5
Y = 5
X == Y // we will get answer 1, as the statement X == Y is true

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 9]
X == Y
// we will get answer [1 0 0 1].  We get 1 on indices 0 and 3 as they are equal. Other indices are 0 as they are not equal

//Comparing Array to Scalar
X = [5 6 7]
Y = 5
X == Y
// we will get answer [1 0 0]. We get 1 on index 0 as it is equal. We get 0 on indices 1 and 2 as they are not equal



### Not equal to (~=)

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.

//Comparing scalar to scalar
X = 5
Y = 5
X ~= Y
// we will get answer 0

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 9]
X ~= Y
// we will get answer [0 1 1 0]

//Comparing Array to Scalar
X = [5 6 7]
Y = 5
X ~= Y
// we will get answer  [0 1 1]



### Greater than (>)

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.

//Comparing scalar to scalar
X = 6
Y = 5
X > Y
// we will get answer = 1

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 10]
X > Y
// we will get answer [0 0 1 0]

//Comparing Array to Scalar
X = [5 6 7]
Y = 5
X > Y
// we will get answer  [0 1 1]


### Greater than or equal to (>=)

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.

//Comparing scalar to scalar
X = 6
Y = 5
X >= Y // we will get ans = 1 as X is greater than Y.

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 10]
X >= Y
// we will get answer  [1 0 1 0]

//Comparing Array to Scalar
X = [5 6 7]
Y = 5
X >= Y
// we will get answer [1 1 1].



### Less than (<)

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 scalar
X = 6
Y = 5
X < Y
// we will get answer 0

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 10]
X < Y
// we will get answer [0 1 0 1]

//Comparing Array to Scalar
X = [5 2 3]
Y = 5
X < Y
// we will get answer [0 1 1]



### Less than or equal to (<=)

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 scalar
X = 6
Y = 5
X <= Y
// we will get answer 0

//Comparing Array to Array
X = [5 6 7 9]
Y = [5 7 6 10]
X <= Y
// we will get answer [1 1 0 1]

//Comparing Array to Scalar
X = [5 6 7]
Y = 5
X <= Y
// we will get answer [1 0 0].



RELATED TAGS

matlab
communitycreator
relationaloperator

CONTRIBUTOR Zain Ali Babar
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 