Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Logical Or

  1. Aug 30, 2011 #1
    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. jcsd
  3. Aug 30, 2011 #2

    uart

    User Avatar
    Science Advisor

    [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
  4. Aug 30, 2011 #3
    yeah, cool.... my bad

    >> 3 == (4|5)

    ans =

    0
     
  5. Aug 30, 2011 #4

    uart

    User Avatar
    Science Advisor

    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 (\)

    5. Addition (+), subtraction (-)

    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 (||)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab Logical Or
  1. Mathematical logic (Replies: 0)

  2. Matlab logic help (Replies: 3)

  3. MATLAB problem (Replies: 1)

  4. MATLAB histogram (Replies: 3)

Loading...