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

  • MATLAB
  • Thread starter brydustin
  • Start date
In summary, the conversation discusses the behavior of combining the equals operator (==) with logical OR (|) in Matlab. It is noted that this combination will always result in either True (1) or False (0), depending on the notation used. It is also mentioned that the notation choice can affect the result for arrays, with || being better for single elements. The conversation includes examples of this behavior and discusses operator precedence in Matlab.
  • #1
brydustin
205
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
  • #2
[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:
  • #3
yeah, cool... my bad

>> 3 == (4|5)

ans =

0
 
  • #4
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 (||)
 
  • #5


Hi there,

Thank you for your question. I can understand why you may be confused about the results you are getting when comparing '==' and 'or'. Let me explain why you are getting these results.

Firstly, let's take a look at the different notations you mentioned - 'or(A,B)' and 'A||B'. These notations are used for different purposes. 'or(A,B)' is used for performing logical operations on arrays, while 'A||B' is used for performing logical operations on single elements. So, if you are dealing with arrays, you should use 'or(A,B)' notation, and if you are dealing with single elements, you should use 'A||B' notation.

Now, let's look at the results you are getting. When you are comparing '3 == 4|5', the result is 1 because the logical operation 'or' is performed on two single elements, 4 and 5. Since both 4 and 5 are not equal to 3, the result is 1 (True).

Similarly, when you are comparing '3 == 4||5', the result is also 1 because the logical operation '||' is performed on two single elements, 4 and 5. Again, since both 4 and 5 are not equal to 3, the result is 1 (True).

However, when you are comparing '3 == or(3,3)', the result is 0 because the logical operation 'or' is performed on an array, [3,3]. In this case, the array is checked element-wise, and since both elements in the array are equal to 3, the result is 0 (False).

Similarly, when you are comparing '3 == or(-Inf,4)', the result is 0 because the logical operation 'or' is performed on an array, [-Inf,4]. In this case, the first element in the array is not equal to 3, so the result is 0 (False).

Lastly, when you are comparing '3 == or(2,3)', the result is 0 because the logical operation 'or' is performed on an array, [2,3]. In this case, the second element in the array is equal to 3, so the result is 0 (False).

In conclusion, the results you are getting are not wrong, and you are not missing anything. It is just
 

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

1. What is the difference between using '==' and 'or' for comparisons in Python?

The '==' operator is used to compare if two values are equal in Python, while 'or' is a logical operator used to combine two conditions and return a single boolean value.

2. Can '==' and 'or' be used interchangeably for comparisons in Python?

No, they cannot. '==' is used for value comparison, while 'or' is used for logical comparison. They serve different purposes and cannot be used interchangeably.

3. Is there a preference for using '==' or 'or' in Python?

It depends on the context of the comparison. If you want to compare two values for equality, then '==' should be used. If you want to check if one of two conditions is true, then 'or' should be used.

4. Are there any performance differences between using '==' and 'or' for comparisons in Python?

There may be slight performance differences, but they are negligible. It is more important to use the appropriate operator for the desired comparison rather than worrying about performance.

5. Is there a way to combine '==' and 'or' in a single comparison in Python?

Yes, you can use the 'or' operator with the '==' operator to combine multiple conditions in a single comparison. For example, x == 5 or x == 10 will return True if x is either 5 or 10.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Precalculus Mathematics Homework Help
Replies
16
Views
719
  • Precalculus Mathematics Homework Help
Replies
14
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • General Math
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top