Categories

# Matlab boolean

MATLAB Boolean operators are used to return logical values (True for 1 and False for 0) in case we want to check if a condition is met or not. Boolean operators are very useful in codes where we need to execute code lines based on certain conditions.

For example, we can compare 2 numbers using logical operators and get True in the output if the numbers are equal else False.

## Syntax:

Logical AND (&), OR (|), NOT (~) are some of the commonly used Boolean operators.

### How to use Matlab boolean with Examples?

Let us now understand how to use logical or Boolean operators in MATLAB. We will discuss 2 scenarios:

1. The use of Boolean operators in arrays
2. The use of Boolean operators in circuits

## Example 1:

In this example, we will use an ‘&’ operator between 2 matrices. An ‘&’ operator will give ‘1’ as the output if the corresponding elements in both the matrices are non-zero, else it will give ‘0’ as the output. The steps to be followed for this example are:

1. Initialize the input matrices
2. Use the ‘&’ operator between the matrices

## Code:

``````A = [5 4 1; 5 2 0; 1 0 1] [Initializing the 1st matrix] B = [0 1 1; 0 0 0; 1 9 3] [Initializing the 2nd matrix] A & B
[Using the Boolean operator ‘&’ between the matrices. The output will be a matrix of 0s and 1s]``````

This is how our input and output will look like in MATLAB:

## Input:

``A = [5 4 1; 5 2 0; 1 0 1] B = [0 1 1; 0 0 0; 1 9 3] A & B``

## Output:

As we can see in the output, the Boolean operator ‘&’ gives ‘1’ as the output if the corresponding elements in both the matrices are non-zero, else it gives ‘0’ as the output

Next, we will see the use of the ‘&’ operator in the circuits

### Example 2:

When used in circuits, the ‘&’ operator will give ‘1’ as the output if both the variables are non-zero, else it will give ‘0’ as the output. The steps to be followed for this example are:

1. Initialize the input variables
2. Use the if-else loop along with the Boolean ‘&’ operator

## Code:

``````a = 10
[Initializing the 1st variable] b = 20
[Initializing the 2nd variable] c = 0
[Initializing the 3rd variable] if (a && b)
disp(‘Both the numbers are non-zero’);
else
disp(‘Both the numbers are not non-zero’);
end
[This loop with Boolean ‘&’ operator will check if both the variables are non-zero; In this example we have taken both ‘a’ and ‘b’ as non-zero] if (a && c)
disp(‘Both the numbers are non-zero’);
else
disp(‘Both the numbers are not non-zero’);
end
[This loop will check if both the variables ‘a’ and ‘c’ are non-zero]``````

This is how our input and output will look like in MATLAB:

## Input:

``````a = 10
b = 20
c = 0
if (a && b)
disp('Both the numbers are non-zero');
else
disp('Both the numbers are not non-zero');
end
if (a && c)
disp('Both the numbers are non-zero');
else
disp('Both the numbers are not non-zero');
end``````

## Output:

• In the 1st loop, both the variables ‘a’ and ‘b’ are non-zero, so the output after using Boolean ‘&’ operator is “Both the numbers are non-zero”
• In the 2nd loop, the variables ‘a’ is non-zero, whereas the variable ‘c’ is zero, so the output after using Boolean ‘&’ operator is “Both the numbers are not non-zero”

### Example 3:

In this example, we will use an ‘|’ (OR) operator between 2 matrices. An ‘|’ operator will give ‘1’ as the output if any one of the corresponding elements in the matrices is non-zero, else it will give ‘0’ as the output. The steps to be followed for this example are:

1. Initialize the input matrices
2. Use the ‘|’ operator between the matrices

## Code:

``````<!-- wp:paragraph -->
<p><code>A = [2 4 6; 2 2 0; 3 0 7] [Initializing the 1<sup>st</sup>&nbsp;matrix] B = [0 4 1; 1 0 0; 2 0 3] [Initializing the 2<sup>nd</sup>&nbsp;matrix] A | B<br>[Using the Boolean operator ‘|’ between the matrices. The output will be a matrix of 0s and 1s]</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is how our input and output will look like in MATLAB:</p>
<!-- /wp:paragraph -->``````

## Input:

``A = [2 4 6; 2 2 0; 3 0 7] B = [0 4 1; 1 0 0; 2 0 3] A | B``

## Output:

As we can see in the output, the Boolean operator ‘|’ gives ‘1’ as the output if any one of the corresponding elements in the matrices is non-zero, else it gives ‘0’ as the output.

Next, we will see the use of the ‘|’ operator in the circuits

### Example 4:

When used in circuits, the ‘|’ operator will give ‘1’ as the output if any one of the variables is non-zero, else it will give ‘0’ as the output. The steps to be followed for this example are:

1. Initialize the input variables
2. Use the if else loop along with the Boolean ‘|’ operator

## Code:

``````a = 0
[Initializing the 1st variable] b = 10
[Initializing the 2nd variable] c = 0
[Initializing the 3rd variable] d = 0
[Initializing the 4th variable] if (a || b)
disp(‘At least one of the numbers is non-zero’);
else
disp(‘Both the numbers are zero’);
end
[This loop with Boolean ‘|’ operator will check if any one of the variables is non-zero; In this example we have taken ‘a’ as zero and ‘b’ as non-zero] if (c || d)
disp(‘At least one of the numbers is non-zero’);
else
disp(‘Both the numbers are zero’);
end
[This loop will check if one of the variables ‘c’ and ‘d’ is non-zero]``````

This is how our input and output will look like in MATLAB:

## Input:

``````a = 0
b = 10
c = 0
d = 0
if (a || b)
disp('At least one of the numbers is non-zero');
else
disp('Both the numbers are zero');
end
if (c || d)
disp('At least one of the numbers is non-zero');
else
disp('Both the numbers are zero');
end``````

## Output:

• In the 1st loop, the variable ‘a’ is 0 and ‘b’ is non-zero, so the output after using Boolean ‘|’ operator is “At least one of the numbers is non-zero”
• In the 2nd loop, both the variables ‘c’ and ‘d’ are zero, so the output after using Boolean ‘|’ operator is “Both the numbers are zero”