how can I test and display how many values in a vector are contained in a pre-determined interval
조회 수: 8 (최근 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!