EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab boolean
 

Matlab boolean

Updated March 6, 2023

Matlab boolean

 

 

Introduction to 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Matlab boolean output 1

Output:

Matlab boolean output 2

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

Matlab boolean output 3

Output:

output 4

  • 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:

A = [2 4 6; 2 2 0; 3 0 7] [Initializing the 1st matrix] B = [0 4 1; 1 0 0; 2 0 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 = [2 4 6; 2 2 0; 3 0 7] B = [0 4 1; 1 0 0; 2 0 3] A | B

Matlab boolean output 5

Output:

output 6

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 7

Output:

output 8

  • 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”

Conclusion

  • Boolean operators are useful in cases where we want to check if a condition is met or not.
  • Boolean operators are also used in circuit codes where we need to execute code lines based on certain conditions.

Recommended Articles

This is a guide to Matlab boolean. Here we discuss how to use logical or Boolean operators in MATLAB along with the Examples and outputs. You may also look at the following article to learn more –

  1. Matlab limit
  2. Matlab Saveas
  3. Arctan Matlab
  4. Matlab polyfit()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW