Comparing '==' and 'or': True or False?

  • Context: MATLAB 
  • Thread starter Thread starter brydustin
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the behavior of the equality operator (==) in conjunction with the logical OR operator (|) in MATLAB. Participants explore how these operators interact and the implications of operator precedence on the evaluation of expressions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that combining == with logical or will always yield True (1) or False (2), depending on the notation used.
  • Another participant clarifies that the expression 3 == 4|5 is evaluated left to right without precedence, leading to a specific interpretation of the expression.
  • A subsequent post acknowledges a misunderstanding and provides a corrected evaluation of the expression as 3 == (4|5), which results in 0.
  • A participant references MATLAB documentation to outline the operator precedence, indicating that the equality operator (==) has higher precedence than the element-wise OR operator (|).

Areas of Agreement / Disagreement

Participants express differing views on the evaluation of expressions involving == and |, with some clarifying misunderstandings while others provide corrections. The discussion does not reach a consensus on the implications of these evaluations.

Contextual Notes

There are limitations regarding the assumptions made about operator precedence and the specific behavior of logical operators in MATLAB, which may depend on context and usage.

brydustin
Messages
201
Reaction score
0
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
 
Physics news on Phys.org
[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:
yeah, cool... my bad

>> 3 == (4|5)

ans =

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

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K