how can I test and display how many values in a vector are contained in a pre-determined interval
4 ビュー (過去 30 日間)
古いコメントを表示
%her is my vector, I want to make a script that allow to display how many elements in the interval T=[160:192] automatically, so I can later calculate the succes rate this way :
%number_of_elements_in_T * 100 / length(idx);
idx=[168 190 173 147 147 110 169 57 174 122 159 174 104 132];
1 件のコメント
Rik
2019 年 4 月 29 日
Why didn't you put in your vector as text?
idx=[168 190 173 147 147 110 169 57 174 122 159 174 104 132];
採用された回答
Rik
2019 年 4 月 29 日
Trivial with ismember:
idx=[168 190 173 147 147 110 169 57 174 122 159 174 104 132];
T=160:192;
in_T=ismember(idx,T);
number_of_elements_in_T=sum(in_T);
number_of_eliments_out_T=sum(~in_T);
%or:
%number_of_eliments_out_T=numel(idx)-number_of_elements_in_T
score = number_of_elements_in_T * 100 / number_of_eliments_out_T;
If your values in T wouldn't be integers, you should use ismembertol, if your values in idx should be in a range, you could also consider the following setup:
idx=[168 190 173 147 147 110 169 57 174 122 159 174 104 132];
T_bounds=[160 192];
in_T=idx>T_bounds(1) & idx<T_bounds(2);
3 件のコメント
Rik
2019 年 4 月 29 日
If you want to count the number of elements you can use the numel function. I would not suggest the length function, because that is generally not what you mean (a call to size with a second argument tends to solve the problems that numel can't solve).
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!