bin data and sum values

Hi everyone! my problem is I have a vector x and a vector of values at those y for example:
x y
0 0.0040
17.8200 0.0030
19.0700 0.0035
19.1600 0.0028
19.9300 2.0036
20.5400 0.1609
20.5700 0.0275
20.6000 0.0023
20.6400 0.0142
20.6700 20.5603
20.9600 0.0023
21.0800 0.0193
21.1200 0.0098
21.2100 31.5333
21.3100 0.0023
21.3400 0.0012
21.3700 0.0054
21.5000 0.0029
21.6000 15.7993
bins = [0;3.5;6;8;10;12;14;16;18;20;22;24;26;28;30;32;36;44;48;50;60];
[counts] = histc(x,bins);
figure=bar(bins,counts,'histc');
That is what I initially did but I also need for each bin to sum the corresponding y values and at the end create a bar graph showing total y for each bin of x. Any ideas??

 Respuesta aceptada

Roger Stafford
Roger Stafford el 11 de Jun. de 2014

2 votos

[counts,idx] = histc(x,bins);
t = accumarray(idx,y,[size(bins,1),1]);

2 comentarios

nath
nath el 16 de Jun. de 2014
Thank you for your help Roger! it does exactly what i asked for!
Rahul Kesarkar
Rahul Kesarkar el 7 de Abr. de 2021
How would it work if i have to bin data for a particular date range?
For example range : - weekday 12/06/2017 20:00 to 13/06/2017 08:00 and repeat this for every weekday. Weekend 9/06/2017 20:00 to 12/06/2017 08:00. Sum up all the values in the bin.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 11 de Jun. de 2014

Comentada:

el 7 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by