Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between | and || in MATLAB?

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

like image 718
Lay Avatar asked Jan 06 '13 15:01

Lay


People also ask

What is ~= in MATLAB?

example. A ~= B returns a logical array with elements set to logical 1 ( true ) where arrays A and B are not equal; otherwise, the element is logical 0 ( false ). The test compares both real and imaginary parts of numeric arrays. ne returns logical 1 ( true ) where A or B have NaN or undefined categorical elements.

How do you represent or in MATLAB?

The symbols | and || perform different operations in MATLAB®. The element-wise OR operator described here is | . The short-circuit OR operator is || .

How do I check if MATLAB is NaN?

TF = isnan( A ) returns a logical array containing 1 ( true ) where the elements of A are NaN , and 0 ( false ) where they are not. If A contains complex numbers, isnan(A) contains 1 for elements with either real or imaginary part is NaN , and 0 for elements where both real and imaginary parts are not NaN .


1 Answers

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.

like image 170
David Heffernan Avatar answered Nov 15 '22 03:11

David Heffernan