conditional 'if else' statementS

1 回表示 (過去 30 日間)
Aadhar Anand
Aadhar Anand 2021 年 4 月 17 日
コメント済み: Atsushi Ueno 2021 年 4 月 17 日
hi , can you please tell me , whether we can use colon operator (:) in if statements or not? if yes, can you please write a simple code.

回答 (1 件)

Atsushi Ueno
Atsushi Ueno 2021 年 4 月 17 日
I should say NO for this question. But there are various way to judge each elements of vector.
I think you require 3 judgements (from 3 to 5) for this example, but the result is only once 'FALSE exist'.
It means if([true true false]) is judged as false.
a = ones(1,10);
a(5) = 0;
if(a(3:5))
disp 'all TRUE';
else
disp 'FALSE exist';
end
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 17 日
if(a(3:5))
That is a valid example of using : in an if statement, so the answer should be YES, it can be used.
Other examples:
if all('abc' == 97:99)
if a(:) * 2.^(0:3) == 11 %converts 4 bit binary to decimal and compares to 11
Atsushi Ueno
Atsushi Ueno 2021 年 4 月 17 日
It's a matter of wording.
Yes, we can use colon operator (:) in if statements, but No, it cannot separate if statement and else statement for judgement of each element.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by