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.

 Akzeptierte Antwort

Steven Lord
Steven Lord am 28 Apr. 2020

0 Stimmen

Instead of building your own binning function, consider using histcounts or perhaps histcounts2.

3 Kommentare

Alexander Arzon
Alexander Arzon am 28 Apr. 2020
I haven't heard of binning or this function before; following your suggestion, I've read about the function in MATLAB help, but how I can apply it for my intended use isn't obvious. Could you elaborate?
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 am 29 Apr. 2020
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by