how can I test and display how many values in a vector are contained in a pre-determined interval

8 vues (au cours des 30 derniers jours)
%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];

Réponse acceptée

Rik
Rik le 29 Avr 2019
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 commentaires
Rik
Rik le 29 Avr 2019
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 le 30 Avr 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by