How to create vectors out of (histogram) bins and take averages?
    7 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commenti
Risposte (1)
  Steven Lord
    
      
 il 8 Ott 2021
        You can use groupsummary.
x = rand(10, 1);
y = rand(10, 1);
data = table(x, y) % For display
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)
n = 2; % Check bin by manual computation
check = mean(y(edges(n) <= x & x < edges(n+1)))
check == M(n) % true
2 Commenti
  Steven Lord
    
      
 il 10 Ott 2021
				That's fine. That means the grouping variable and the data variable will be the same.
x = rand(10, 1)
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)
n = 2; % Check bin by manual computation
check = mean(x(edges(n) <= x & x < edges(n+1)))
check == M(n) % true
Vedere anche
Categorie
				Scopri di più su Histograms in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!