Conditional Question Matlab Disagreement

조회 수: 8 (최근 30일)
Jayden
Jayden 2023년 10월 24일
댓글: Fabio Freschi 2023년 10월 24일
(6+3)>8>2 - I said the answer would be zero but my teacher says it’s one and I’m super confused. Any explanation would be appreciated, thanks!

답변 (3개)

Stephen23
Stephen23 2023년 10월 24일
편집: Stephen23 2023년 10월 24일
"...but my teacher says it’s one and I’m super confused. Any explanation would be appreciated, thanks!"
This code
(6+3)>8>2
ans = logical
0
is parsed from left-to-right and is equivalent to
((6+3)>8)>2
ans = logical
0
which comes down to either of these two cases (both of which return zero):
0>2
ans = logical
0
1>2
ans = logical
0
So even without adding 6+3 we know the result is always zero. In fact, the value of that sum is irrelevant.
Note that MATLAB has one operator >, it does not have a logical operator with three inputs that looks like A>B>C.
  댓글 수: 4
Stephen23
Stephen23 2023년 10월 24일
편집: Stephen23 2023년 10월 24일
"I emailed my teacher and they said “ Work from right to left 8>2 ..."
The MATLAB documentation states "Within each precedence level, operators have equal precedence and are evaluated from left to right." An operator is only in one level, so repetitions of any operator will be parsed from left to right:
For what it is worth, left-to-right is generally a mathematical and computing convention:
Dyuman Joshi
Dyuman Joshi 2023년 10월 24일
I am not sure which notation your teacher is using when saying to work from right to left for the given case in relation to MATLAB.
You should ask for an explaination.

댓글을 달려면 로그인하십시오.


Michael
Michael 2023년 10월 24일
편집: Michael 2023년 10월 24일
I think you are right and your teacher is wrong.
MATLAB evaluates this expression from the left to the right (see chapter "Operator Precedence" in the MATLAB documentation) with respect to the parentheses. That means
  • (6+3) = 9
  • 9 > 8 = true (logical value 1)
  • 1 > 2 = false (logical value 0)
Result of (6+3) > 8 > 2 = false (logical value 0)
  댓글 수: 1
John D'Errico
John D'Errico 2023년 10월 24일
And of course one can always verify that by a simple test.
(6+3) > 8 > 2
ans = logical
0
Trust, but verify.

댓글을 달려면 로그인하십시오.


Fabio Freschi
Fabio Freschi 2023년 10월 24일
I agree with all the comments and answers. But maybe one should read (6+3)>8>2 as "is 8 larger than 2 and lower than (6+3)?" that translates in
(6+3) > 8 && 8 > 2
ans = logical
1
  댓글 수: 4
Steven Lord
Steven Lord 2023년 10월 24일
FYI if you enter that expression in a file in MATLAB Editor, Code Analyzer will call out that it thinks that's what you intended to ask ("is 8 larger than 2 and lower than (6+3)?") and will display a Code Analyzer warning suggesting your alternative as a way to actually ask that question. I've attached a script file containing that expression:
dbtype example2037936.m
1 y = (6+3) > 8 > 2;
The codeIssues object will list the Code Analyzer messages for files or folders.
issues = codeIssues('example2037936.m');
Let's display the description of the only Code Analyzer message for that file, wrapped to 70 characters per line (so you can see the whole message without scrolling.)
string(textwrap(issues.Issues.Description, 70))
ans = 3×1 string array
"Expressions like a > b > c are interpreted as (a > b) > c. Typically, " "to test a > b > c mathematically, if all arguments are numeric " "scalars, use (a > b) && (b > c), otherwise use (a > b) & (b > c)."
Fabio Freschi
Fabio Freschi 2023년 10월 24일
Very interesting and useful comment: thank you @Steven Lord!
I guess I will never see that comment because a condition like that will never cross my mind!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by