Matlab Logical Or

1. Aug 30, 2011

brydustin

It seems as if combining == with logical or will always give True (1) or False (2) whenever you perform the simple check, its result is only dependent on choice of notation or(A,B) versus A||B (or A|B which is better for arrays, generally || is for single elements)

Is there something wrong? or am I totally missing something, thanks....

(source for equivalent notation: http://www.mathworks.co.uk/help/techdoc/ref/logicaloperatorselementwise.html)

>> 3 == 4|5

ans =

1

>> 3 == 4||5

ans =

1

>> 3 == 3||3

ans =

1

>> 3 == or(3,3)

ans =

0

>> 3 == or(-Inf,4)

ans =

0

>> 3 == or(2,3)

ans =

0

2. Aug 30, 2011

uart

[strike]It appears that 3 == 4|5 is simply evaluated left to right without precedence as (3 == 4)|5[/strike]

Correction: 3 == 4|5 is evaluated with the operator precedence shown below. (Same end result as above of course, "|" processed after "==").

Last edited: Aug 30, 2011
3. Aug 30, 2011

brydustin

>> 3 == (4|5)

ans =

0

4. Aug 30, 2011

uart

From the Matlab documentation the operator precedence is as follows.

Operators are shown in this list, ordered from highest precedence level to lowest precedence level:

1. Parentheses ()

2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)

3. Unary plus (+), unary minus (-), logical negation (~)

4. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)

6. Colon operator (:)

7. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)

8. Element-wise AND (&)

9. Element-wise OR (|)

10. Short-circuit AND (&&)

11. Short-circuit OR (||)