better than if else conditions

1 回表示 (過去 30 日間)
UNK
UNK 2018 年 10 月 28 日
編集済み: Bruno Luong 2018 年 10 月 28 日
I would like to give a tag to a number based on in which region it lies. I am currently using an if else condition. Wondering if there is an easier way.
Note: the intervals are not equal.
Here is the code:
for i = 1:length(data_rot(:,1))
check = acos(data_rot(i,6));
if check >= 0 && check <.6435
tagsPhi(i,1) = 1;
elseif check >= .6435 && check <.9273
tagsPhi(i,1) = 2;
elseif check >= .9273 && check <1.1593
tagsPhi(i,1) = 3;
elseif check >= 1.1593 && check <1.3694
tagsPhi(i,1) = 4;
elseif check >= 1.3694 && check <1.5708
tagsPhi(i,1) = 5;
elseif check >= 1.5708 && check <1.7722
tagsPhi(i,1) = 6;
elseif check >= 1.7722 && check <1.9823
tagsPhi(i,1) = 7;
elseif check >= 1.9823 && check <2.2143
tagsPhi(i,1) = 8;
elseif check >= 2.2143 && check <2.4981
tagsPhi(i,1) = 9;
else
tagsPhi(i,1) = 10;
end
end

採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 28 日
編集済み: Bruno Luong 2018 年 10 月 28 日
...
edges= [0,.6435,.9273,1.1593,1.3694,1.570,1.7722,1.9823,2.2143,2.4981,Inf];
[~,~,tagsPhi] = histcounts(check,edges)

その他の回答 (1 件)

John D'Errico
John D'Errico 2018 年 10 月 28 日
編集済み: John D'Errico 2018 年 10 月 28 日
Look at the third output argument from the function histcounts. It does everything you need, in a fully vectorized form. Yes, I suppose it is not fully obvious you should have known to have looked there. Older releases of MATLAB would use histc.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by