bin data and sum values

11 views (last 30 days)
nath
nath on 11 Jun 2014
Commented: Rahul Kesarkar on 7 Apr 2021
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??

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!