How can I get the number of values within a range?

조회 수: 6 (최근 30일)
areshamal
areshamal 2023년 6월 2일
댓글: Dyuman Joshi 2023년 6월 2일
Hi, I am trying to get the number of values within a range, but this range is moving. For example, I have a value a as the center of my range. So I define my range as a+std. So I get how many values are within such a range. Then I move the range by a shift, such as my new range si (a+shift)+std. The next range will be (a+2shift)+std, and so on. What I have in my code is the following:
lo_edge=center_range-std;
hi_edge=center_range+std;
bunchcenter=center_range;
kk=0;
while bunchcenter<colum_of_int(end)
kk=kk+1
bunchcenter=center_range+kk*shift;
number_values= colum_of_int>=lo_edge & column_of_int<=hi_edge;
end
I would be thankful to have some help.
  댓글 수: 2
Luca Ferro
Luca Ferro 2023년 6월 2일
편집: Luca Ferro 2023년 6월 2일
so what is the question? is your code not working? if so please describe the results you get and the desired result you would want. Also, if you can, share the data as well
areshamal
areshamal 2023년 6월 2일
Hi Luca, yes, I forgot to say that I am getting only zeros. I am expecting to have zeros in most of the ranges, in some ranges one element, and in very rare cases 2 or 3 elements.
The value I am using as center_range is 132.74 and shift 265.48. The std is 36.15.
I attach the data I am working with. It is time data given in ns.

댓글을 달려면 로그인하십시오.

답변 (1개)

Pramil
Pramil 2023년 6월 2일
편집: Pramil 2023년 6월 2일
Your code is almost correct. You just need to update the values of lo_edge and hi_edge inside the loop to reflect the new range. Here is the updated code:
lo_edge = center_range - std;
hi_edge = center_range + std;
bunchcenter = center_range;
kk = 0;
while bunchcenter < colum_of_int(end)
kk = kk + 1
bunchcenter = center_range + kk * shift;
lo_edge = bunchcenter - std; % update lo_edge
hi_edge = bunchcenter + std; % update hi_edge
number_values = colum_of_int >= lo_edge & colum_of_int <= hi_edge;
num_values_within_range(kk) = sum(number_values);
end
Hope it helps :)
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 6월 2일
Note - Do not use the names of inbuilt function as names for variables. It is not a good practice. Either capitalize std or better, rename it completely.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by