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.
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:
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:
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:
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.
4.5 (6,995 ratings)
View Course
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 –