フィルターのクリア

switches with the < and > operators

2 ビュー (過去 30 日間)
Francesco Pio
Francesco Pio 2023 年 7 月 17 日
コメント済み: Steven Lord 2023 年 7 月 17 日
Is there a way to use the switch construct by checking with operators like > and < and not doing an equality check?
for example :
x = 12;
switch x
case x < 3
% statementents
case x >= 3 && x < 10
% statements
case x >= 10 && x < 20
% statements
otherwise
% statements
end
Why does it always execute the statements in the otherwise section? What's wrong?

回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 7 月 17 日
A switch statement executes the first case that is true. Try this instead.
x = 12;
switch true
case x < 3
% statementents
case x >= 3 && x < 10
% statements
case x >= 10 && x < 20
% statements
otherwise
% statements
end
  2 件のコメント
Francesco Pio
Francesco Pio 2023 年 7 月 17 日
This works, thanks a lot
Steven Lord
Steven Lord 2023 年 7 月 17 日
Or discretize your data and then switch on the bin into which your data falls.
x = 12;
whichBin = discretize(x, [-Inf 3 10 20 Inf])
whichBin = 3
x = 42;
whichBin = discretize(x, [-Inf 3 10 20 Inf])
whichBin = 4

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

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by