bin data and sum values

조회 수: 8 (최근 30일)
nath
nath 2014년 6월 11일
댓글: Rahul Kesarkar 2021년 4월 7일
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