Data Extraction using Logical Operators

조회 수: 5 (최근 30일)
Alexander Arzon
Alexander Arzon 2020년 4월 28일
댓글: Alexander Arzon 2020년 4월 29일
Trying to write code that will extract data from a set between two values and store the number of times data in the set matches the criteria into an array, this is the code that I have:
v = (1:1:20) ; %wind speed
n = 0;
hours1 = zeros();
hours2 = zeros();
for n = 1:length(v)
temp = 0;
temp2 = 0;
if(wind_adj1 < n & wind_adj1 > n - 1)
temp = 1 ;
if(wind_adj2 < n & wind_adj2 > n - 1)
temp2 = 1;
end
end
hours1(n+1,1) = temp;
hours2(n+1,1) = temp2;
end
For thie first iteration of the loop, I'd like to know how many cells from the dataset are between 0 m/s and 1 m/s (the data is hourly wind speed.) I'd like for the loop to continue to 20 m/s and the intended use of the data is to generate a Rayleigh probability density function (pdf.) I'm confident that the issue is in how I'm structuring the logical check, but I've worked on it for a few hours and haven't cracked it. Any help or advice the could be given would be appreciated.

채택된 답변

Steven Lord
Steven Lord 2020년 4월 28일
Instead of building your own binning function, consider using histcounts or perhaps histcounts2.
  댓글 수: 3
Steven Lord
Steven Lord 2020년 4월 29일
x = 1:0.5:5;
thecounts = histcounts(x, [1 3 4 6])
The first element of thecounts is how many elements in x are greater than or equal to 1 and less than 3.
The second element of thecounts is how many elements in x are greater than or equal to 3 and less than 4.
The third element of thecounts is how many elements in x are greater than or equal to 4 and less than or equal to 6. [The last bin is special in that it contains both its ends, not just the left end.]
If I understand your question correctly, this is exactly what you want to know about your data set.
Alexander Arzon
Alexander Arzon 2020년 4월 29일
I ended up extracting the data that I need from excel, but I follow your logic and this would have accomplished what I had in mind, thank you for your time!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by