bin data and sum values

15 次查看(过去 30 天)
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??

采纳的回答

Roger Stafford
Roger Stafford 2014-6-11
[counts,idx] = histc(x,bins);
t = accumarray(idx,y,[size(bins,1),1]);
  2 个评论
nath
nath 2014-6-16
Thank you for your help Roger! it does exactly what i asked for!
Rahul Kesarkar
Rahul Kesarkar 2021-4-7
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by