How to create vectors out of (histogram) bins and take averages?

5 Ansichten (letzte 30 Tage)
Lu Da Silva
Lu Da Silva am 8 Okt. 2021
Kommentiert: Steven Lord am 10 Okt. 2021
I have a vector containing speeds from 0.25m/s to 20m/s. I need to bin the speeds in bins of 0.5m/s widths cenetred at integers (e.g. bin 1: from 0.25m/s to 0.75m/s, bin 2 from 0.75m/s to 1.25m/s, bin 3 from 1.25m/s to 1.75m/s, etc.). How can I make these bins and take the average of each and create a vector containing the mean values of each bin?
I managed to plot it in a histogram:
edges = 0.25:0.5:20;
h = histogram(ws,edges); % ws = wind speed vector
but I need a vector for each bin and its average.
  2 Kommentare
KSSV
KSSV am 8 Okt. 2021
See the variable h, it got all your information you wanted.
Lu Da Silva
Lu Da Silva am 10 Okt. 2021
@KSSV Maybe I'm not seeing it... Yes there are some specifics of the histogram in 'h' but my question is how do I get the average of each bin?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 8 Okt. 2021
You can use groupsummary.
x = rand(10, 1);
y = rand(10, 1);
data = table(x, y) % For display
data = 10×2 table
x y ________ ________ 0.2421 0.88207 0.74513 0.091348 0.011883 0.88123 0.67133 0.56919 0.56695 0.79875 0.9975 0.27806 0.74134 0.091931 0.56092 0.12012 0.73913 0.64931 0.30515 0.17256
edges = 0:0.25:1;
% Take the mean of subsets of y corresponding to x values that fall between
% two elements of the edges vector
M = groupsummary(y, x, edges, @mean)
M = 4×1
0.8817 0.1726 0.3868 0.2781
n = 2; % Check bin by manual computation
check = mean(y(edges(n) <= x & x < edges(n+1)))
check = 0.1726
check == M(n) % true
ans = logical
1
  2 Kommentare
Lu Da Silva
Lu Da Silva am 10 Okt. 2021
I don't have 2 data sets (x and y), I only have the wind speed
Steven Lord
Steven Lord am 10 Okt. 2021
That's fine. That means the grouping variable and the data variable will be the same.
x = rand(10, 1)
x = 10×1
0.6509 0.3036 0.2178 0.6356 0.9714 0.3121 0.7992 0.2033 0.9275 0.0883
edges = 0:0.25:1;
% Take the mean of subsets of x corresponding to x values that fall between
% two elements of the edges vector
M = groupsummary(x, x, edges, @mean)
M = 4×1
0.1698 0.3078 0.6432 0.8994
n = 2; % Check bin by manual computation
check = mean(x(edges(n) <= x & x < edges(n+1)))
check = 0.3078
check == M(n) % true
ans = logical
1

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by