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 OR Operator
 

Matlab OR Operator

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

Matlab OR Operator

 

 

Introduction to Matlab OR Operator

Logical operators are the operators that control the flow of execution in a program based on the decisions i.e. either True or False (0 or 1). They are very simple to use and understand. They can be used in the conditional statements also to execute a certain statement if it matches a certain criterion. There are three types of logical operators in any programming language i.e. AND (P & Q), OR (P | Q), NOT(~P). They are also used to display the array elements in the form of 0 and 1 if it matches a certain condition. Here we will discuss the working and examples of Matlab OR Operator in detail.

Watch our Demo Courses and Videos

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

Working of Matlab OR Operator

In Matlab, logical operators work in the same way as in other programming languages. Logical OR operator results in true or false based on the inputs that are given to the input signal. They are denoted by | operator (A|B). Please find the below table to understand the working of a logical OR operator in Matlab.

Truth Table:

Input 1

   (P)

Input 2

   (Q)

Output

(P|Q)

    0

    0

   0

    0

    1

   1

    1     0

   1

    1

    1

   1

The above table explains the working of the OR operator. There are two inputs (P and Q) and based on these two values, the output value is decided. When both the values are false, it results in false i.e. 0. Similarly, when both values are true (1) it results in True I.e.1 and when one of the input values is true it results in 1 or true.

Examples to Implement Matlab OR Operator

Below are the examples to understand Matlab OR operator in detail:

Example #1

P = [1,1,0,0,1,0] Q = [0,0,1,0,1,0] P|Q

Output:

Matlab OR Operator eg1

Logical OR operator follows short-circuiting principle in Matlab i.e. the second part of the expression is not checked or evaluated if the first part of the expression results in true or 1 because eventually, the final expression will be true since one of the value is true. It is denoted by the “||” symbol. This short-circuiting principle is different in AND operator i.e. the second part of the expression is not checked or evaluated if the first part of the expression results in false or 0. It gives the final result as scalar output i.e. 1 or 0. Please find the below examples for better understanding:

Example #2

U= [0,1,0,0,1,0] V= [0,1,0,1,1,1] any(U)||any(V)

Output:

Matlab OR Operator eg3

Here the short-circuiting principle is used i.e. if the first part of the expression matches the condition, it will not check the second part. Here the first part of the expression evaluates to True since there is logical 1 present in the first array. Any function checks if the array contains at least one of the elements as 1 which is true here so, it doesn’t check the second part of the expression and results in 1.

This is known as a short-circuiting principle because it gives the output without checking the complete expression and gives the result based on the first part of the expression i.e. short-circuited. We should always use || and && operator for checking or evaluating the short circuit expressions to get the desired result. Logical OR operators are also useful in finding if the matrix contains 0 values or not. The matrix will result in 1 if any of the values either of the arrays is not zero and it will result in 0 if both the values in the array are 0. Please find the below example for your reference:

Example #3

P= [ 3,6,0;8,7,0;0,1,2] Q= [3,5,0;2,1,0;0,0,4] P|Q

Output:

Example 3

In the above example, the outputs are displayed in terms of logical values. So both the values from P and Q are checked and it results in 1 or 0. For example, if we compare the first elements of P and Q i.e. 3 and 3 since both the values are not zero, it will result in 1. If we compare the third element and first row of P and Q, since both the elements are 0, the resulting output will be 0.

The input can be matrix, vector, scalar or multi-dimension array depending on the requirements. We can also use all logical operators in a single expression to get the output. We should always remember that | and || have different working mechanisms in Matlab. If we use normal operators like & or | while evaluating in looping and conditional statements, they always follow the short-circuiting principle in checking the expression. So, it is better to use && and || operator while working with conditional or looping expressions.

Conclusion

Logical operators are used in many programming languages for various requirements in the business. They operate in their order of precedence if all of them are operated in a single expression and & operator over-rules OR (|) operator while executing it.

Recommended Articles

This is a guide to Matlab OR Operator. Here we discuss the Introduction and working of Matlab OR Operator along with examples and code implementation. You can also go through our suggested articles to learn more –

  1. What are the Matlab Commands?
  2. Examples of Matlab AND Operator
  3. Learn the Top 10 Advantages of Matlab
  4. Introduction to Matrix in Matlab

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