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
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
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
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).
zakaria debih
zakaria debih 2019 年 4 月 30 日
in fact it is not really the length which I use, it is another variable called "Nb_test" which decide the length of the vector each time I run the script.
act1_rcn=act1_success*100/Nb_test;
act2_rcn=act2_success*100/Nb_test;
act3_rcn=act3_success*100/Nb_test;
act4_rcn=act4_success*100/Nb_test;
act5_rcn=act5_success*100/Nb_test;
act6_rcn=act6_success*100/Nb_test;
so I thought it's better to use Nb_test since it will always have the same value given by numel for each vector.
I appreciate your help. thanks again Sir.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by