What's the difference between | and || in MATLAB?
Asked Answered
A

5

5

What is the difference between the | and || logical operators in MATLAB?

Amidships answered 6/1, 2013 at 15:4 Comment(2)
Take a look at the documentation: mathworks.co.uk/help/matlab/matlab_prog/operators.html#f0-38948.Olivas
related question: What's the difference between & and && in MATLAB?Tempe
I
5

I'm sure you've read the documentation for the short-circuiting operators, and for the element-wise operators.

One important difference is that element-wise operators can operate on arrays whereas the short-circuiting operators apply only to scalar logical operands.

But probably the key difference is the issue of short-circuiting. For the short-circuiting operators, the expression is evaluated from left to right and as soon as the final result can be determined for sure, then remaining terms are not evaluated.

For example, consider

x = a && b

If a evaluates to false, then we know that a && b evaluates to false irrespective of what b evaluates to. So there is no need to evaluate b.

Now consider this expression:

NeedToMakeExpensiveFunctionCall && ExpensiveFunctionCall

where we imagine that ExpensiveFunctionCall takes a long time to evaluate. If we can perform some other, cheap, test that allows us to skip the call to ExpensiveFunctionCall, then we can avoid calling ExpensiveFunctionCall.

So, suppose that NeedToMakeExpensiveFunctionCall evaluates to false. In that case, because we have used short-circuiting operators, ExpensiveFunctionCall will not be called.

In contrast, if we used the element-wise operator and wrote the function like this:

NeedToMakeExpensiveFunctionCall & ExpensiveFunctionCall

then the call to ExpensiveFunctionCall would never be skipped.

In fact the MATLAB documentation, which I do hope you have read, includes an excellent example that illustrates the point very well:

x = (b ~= 0) && (a/b > 18.5)

In this case we cannot perform a/b if b is zero. Hence the test for b ~= 0. The use of the short-circuiting operator means that we avoid calculating a/b when b is zero and so avoid the run-time error that would arise. Clearly the element-wise logical operator would not be able to avoid the run-time error.

For a longer discussion of short-circuit evaluation, refer to the Wikipedia article on the subject.

Idea answered 6/1, 2013 at 15:15 Comment(1)
+1: It might be noteworthy to mention that | can operate on arrays too, while || only on scalars. Moreover, Loren Shure points out here that | can short-circuit too (!!), but only in if/while statements.Tempe
S
3

Logical Operators

MATLAB offers three types of logical operators and functions:

  • | is Element-wise — operate on corresponding elements of logical arrays.
    Example: vector inputs A and B

    A = [0 1 1 0 1]; B = [1 1 0 0 1];

    A | B = 11101

  • || is Short-circuit — operate on scalar, logical expressions

    Example:

    || : Returns logical 1 (true) if either input, or both, evaluate to true, and logical 0 (false) if they do not.

    Operand: logical expressions containing scalar values.

    A || B (B is only evaluated if A is false)

    A = 1; B = 0;

    C =(A || (B = 1));

    B is 0 after this expression and C is 1.

  • Other is, Bit-wise — operate on corresponding bits of integer values or arrays.
    reference link

Surveying answered 6/1, 2013 at 15:11 Comment(0)
P
0

|| is used for scalar inputs

| takes array input in if/while statements

From the source:-

Always use the && and || operators when short-circuiting is required. Using the elementwise operators (& and |) for short-circuiting can yield unexpected results.

Pizarro answered 6/1, 2013 at 15:9 Comment(1)
This is not entirely accurate.Tempe
K
0

| represents OR as a logical operator. || is also a logical operator called a short-circuit OR

The most important advantage of short-circuit operators is that you can use them to evaluate an expression only when certain conditions are satisfied. For example, you want to execute a function only if the function file resides on the current MATLAB path. Short-circuiting keeps the following code from generating an error when the file, myfun.m, cannot be found:

 comp = (exist('myfun.m') == 2) && (myfun(x) >= y)

Similarly, this statement avoids attempting to divide by zero:

x = (b ~= 0) && (a/b > 18.5)

You can also use the && and || operators in if and while statements to take advantage of their short-circuiting behavior:

if (nargin >= 3) && (ischar(varargin{3})) 
Kofu answered 6/1, 2013 at 15:14 Comment(2)
I am on a freaked out network on mobile and didn't even see there's another answer. Can someone please format the code in my post.Kofu
Great I guess I have to do it on my own. Here is a another post you may want to look at. I'll format the post when I get front of a machine. #1379915Kofu
M
0

Short-circuit || means, that parameters will be evaluated only if necessarily in expression. In our example expr1 || expr2 if expr1 evaluates to TRUE, than there is no need to evaluate second operand - the result will be always TRUE. If you have a long chain of Short-circuit operators A || B || C || D and your first evaluates to true, then others won't be evaluated.

If you substitute Element-wise logical | to A | B | C | D then all elements will be evaluated regardless of previous operands.

Meritorious answered 6/1, 2013 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.