Using function any with multiple conditions

11 ビュー (過去 30 日間)
JR
JR 2017 年 7 月 11 日
編集済み: Adam 2017 年 7 月 11 日
Say I have A= [5 11 10 8];
I want to check if
  1. there are any values greater than 9 but not less than 6
  2. there are any values less than 6 but not greater than 9
  3. the array contains values greater than 9 and less than 6
My code looks like this:
if any(A>9)
'do this';
elseif any (A<6);
'do that';
elseif any (A>6 && A<5)
'do this';
end
However, I am getting this error : Operands to the and && operators must be convertible to logical scalar values.
How do I fix
any (A>6 && A<5)
since its the one that is giving me errors. Also is it necessary to use the third
elseif

回答 (1 件)

Adam
Adam 2017 年 7 月 11 日
編集済み: Adam 2017 年 7 月 11 日
That specific error should be fixable just by replacing && with &
The condition itself will always return false though unless what you really want is
any( A > 6 | A < 5 )

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by