How to Use Logical Operator Within If Statements in MATLAB?


In MATLAB, there are three logical operators namely, logical AND (&&), logical OR (||), and logical NOT (~). The "if" is a conditional statement used to develop control expressions in MATLAB programming. In this tutorial, I will explain the use of logical operators within if statements in MATLAB.

What is Logical AND Operator in MATLAB?

In MATLAB, the logical AND operator is a binary operator that takes two operands to execute. It is denoted by "&&".

The result of the logical AND (&&) operator is considered true (1) only if both of its operands or conditions are true (1). If any of the two conditions is false (0), then it will result false (0) as output.

Example 1

Let us understand the working of the logical AND (&&) operator in MATLAB programming with the help of an example.

% MATLAB code to use logical AND operator
% Define some variables
a = 5;
b = 4;
c = 3;
d = 10;

% Using logical AND operator
x = a > 3 && b < 5;
disp(x);

y = c > 5 && d == 10;
disp(y);

Output

It will produce the following output −

1
0

Explanation

In this example, the result "x" is true (1) because both conditions are true (1). The result "y" is false (0), because the first condition is false (0) and the second condition is true (1), resulting false (0) as overall result.

What is Logical OR Operator in MATLAB?

In MATLAB, the logical OR operator is also a binary operator used to perform logical OR operation between two operands. It is denoted by "||".

The logical OR operator (||) produces a result "true (1)" if at least one of its operands is "true (1)". It gives a result "false (0)", only if its both operands are evaluated "false (0)".

Example 2

Let us take an example to understand the working of the logical OR operator (||) in MATLAB.

% MATLAB code to use logical OR operator
% Define some variables
a = 3;
b = 5;
c = 9;
d = 4;

% Using logical OR operator
x = a == 3 || b > 5;
disp(x);

y = c == 3 || d > 5;
disp(y);

Output

It will produce the following output −

1
0

Explanation

In this example, the result "x" is evaluated as true (1) because the first condition is true and the second condition is false. Thus, the overall result is true (1).

The result "y" is false (0), because both the conditions in the expression are false. Hence, the overall result is false.

What is Logical NOT Operator in MATLAB?

In MATLAB, the logical NOT operator is a type of unary operator which takes only one operand to execute.

It is denoted by the symbol "~" (tilde). The logical NOT operator inverts the logical value of the operand. Hence, if the original value of the operand is true (1), the it converts it into false (0), and vice-versa.

Example 3

Let us take an example to understand the execution of the logical NOT operator in MATLAB.

% MATLAB code to use logical NOT operator
% Define a variable
a = 3;

% Using logical NOT operator
x = a > 5; % Define a logical condition
x = ~x;	% Use NOT operator
disp(x);	% Display the NOTed result

y = a == 3; % Define a logical condition
y = ~y;	% Use NOT operator
disp(y);	% Display the NOTed result

Output

It will produce the following output −

1
0

Explanation

In this example, the first result "x" is 1 this is because the defined condition is false (0), the NOT operator inverts it to produce a true (1) as the final result.

Similarly, the second result "y" is false (0) this is because the defined condition is true (1), after using the NOT operator, the final result is false (0).

This is how the logical AND, OR, and NOT operators work in MATLAB.

Example 4

Let us take an example to understand the "if" statement in MATLAB programming.

% MATLAB code to use if statement
% Defining a variable
a = 5;

% Using if statement
if a > 3	% First condition
   disp('First: a is greater than 3.');
else
   disp('First: a is not greater than 3.');
end

if a < 3	% Second condition
   disp('Second: a is less than 3.');
else
   disp('Second: a is not less than 3.');
end

Output

It will produce the following output −

First: a is greater than 3.
Second: a is not less than 3.

Let us now discuss how to use the logical operators, i.e., logical AND operator, logical OR operator, and logical NOT operator in an if statement in MATLAB.

How to Use Logical AND Operator in If Statement in MATLAB?

In MATLAB programming, the logical AND operator (&&) is used to check if multiple conditional statements are true.

Example 5

Let us take an example to understand the use of logical AND operator within an "if" statement in MATLAB.

% MATLAB code to use logical AND operator in an if statement
% Initializing two variables
a = 10;
b = 15;

% Define an if statement with logical AND operator
if a > 5 && b > 5	% First condition
   disp('First: Both conditions are true.');
else
   disp('First: At least one condition is false.');
end

if a > 5 && b > 5	% Second condition
   disp('Second: Both conditions are true.');
else
   disp('Second: At least one condition is false.');
end

Output

It will produce the following output −

First: Both conditions are true.
Second: Both conditions are true.

How to Use Logical OR Operator in If Statement in MATLAB?

In MATLAB, the logical OR operator "||" can also be used in an "if" statement to check multiple conditions.

Example 6

The following example demonstrates the use of logical OR operator in if statement in MATLAB.

% MATLAB code to use logical OR operator in an if statement
% Initializing two variables
a = 10;
b = 15;

% Define an if statement with logical OR operator
if a > 5 || b < 5	% First condition
   disp('First: At least one condition is true.');
else
   disp('First: Both conditions are false.');
end

if a < 5 || b < 5	% Second condition
   disp('Second: At least one condition is true.');
else
   disp('Second: Both conditions are false.');
end

Output

It will produce the following output −

First: At least one condition is true.
Second: Both conditions are false.

How to Use Logical NOT Operator in If Statement in MATLAB?

In MATLAB, the logical NOT operator (~) can also be used within an if statement. It is used to check if a condition is false.

Example 7

The following example demonstrates the use of logical NOT operator in an "if" statement in MATLAB.

% MATLAB code to use logical NOT operator in an if statement
% Initializing a variable
a = input('Enter a number:');

if ~(a > 5)
   disp('The condition is true.');
else
   disp('The condition is false.');
end

Output

It will produce the following output −

Enter a number:
8
The condition is false.
Enter a number:
4
The condition is true.

Explanation

In this example, we have defined an if statement to check if the logical NOT of the condition a > 5 i.e. "~(a > 5)" is true.

When the input value of "a" is greater than 5 (as in first case where a = 8), then the condition ~(a > 5) is false, the if statement is skipped and the instruction inside the else statement is displayed.

In the second case, where a = 4 which is less than 5, thus the condition ~(a > 5) is true. Hence, the code block under the if statement is executed.

Conclusion

In conclusion, MATLAB has three logical operators namely, logical AND operator, logical OR operator, and logical NOT operator. These three operators are used in conditional statements like "if statement". In this tutorial, I explained all these operators and their use in an "if" statement in MATLAB programming.

Updated on: 25-Oct-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements