A compact “if” statement using “or” operator

8 ビュー (過去 30 日間)
Sim
Sim 2023 年 6 月 1 日
コメント済み: Sim 2023 年 6 月 2 日
A more compact way to write the “if” statement using “or” operator, than this one?
if i == 1 || i == 2 || i == 7 || i == 8 || i == 10
% ...
end
Something similar to this statement?
if i == [1 2 7 8 10]
% ...
end

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 6 月 1 日
編集済み: Dyuman Joshi 2023 年 6 月 1 日
Yes there is - ismember
vec = [1 2 7 8 10];
i = 4;
%Checks if elements in i are present in vec or not
ismember(i,vec)
ans = logical
0
if ismember(i,vec)
disp('if')
else
disp('else')
end
else
  7 件のコメント
Sim
Sim 2023 年 6 月 2 日
thanks to everyone, @Dyuman Joshi @Stephen23 @James Tursa @DGM, for your comments!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by