10>6>4

11 ビュー (過去 30 日間)
Carly McKee
Carly McKee 2018 年 9 月 17 日
編集済み: Stephen23 2018 年 9 月 21 日
I am reviewing for a test and one of the questions is "what would be the outcome of 10>6>4?" I thought it would be logical: 1 because all the statements are true, but when I put it into MATLAB it returned logical: 0 for any number I used. Can anyone explain this?

採用された回答

Stephen23
Stephen23 2018 年 9 月 17 日
編集済み: Stephen23 2018 年 9 月 21 日
"Can anyone explain this?"
MATLAB has a binary operator gt, e.g. A>B, but it does not have a ternary operator A>B>C. So your example
10>6>4
is exactly equivalent to this:
(10>6)>4
The part in the parentheses will only ever return the values 0 (false) or 1 (true), so the second comparison will always return false (unless you reduce the value 4 to 0 or less).

その他の回答 (1 件)

Steven Lord
Steven Lord 2018 年 9 月 17 日
Type the following into the MATLAB Editor:
y = 10>6>4;
The number 6 is underlined in orange. Hover over the number 6 to see what the Code Analyzer message says. The details explain how MATLAB interprets that statement and what you should use instead.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by