How can I group data in different size categories?

4 views (last 30 days)
Hi everyone! So I want to categorize the first column of the follwing data according to: [0;3.5;6;8;10;12;14;16;18;20;22;24;26;28;30;32;36;40]
x y
0 0.0040
17.8200 0.0030
19.0700 0.0035
19.1600 0.0028
19.9300 0.0035
20.3800 0.0208
20.4100 0.0002
20.4800 0.6503
20.5100 0.0043
20.5400 0.1609
20.5700 0.0275
20.6000 0.0023
20.6400 0.0142
20.6700 0.0025
20.7000 0.0255
20.7600 0.0028
20.8300 0.0066
20.8600 0.0032
20.9600 0.0023
21.0800 0.0193
21.1200 0.0098
21.2100 0.0112
21.3100 0.0023
21.3400 0.0012
21.3700 0.0054
21.5000 0.0029
21.6000 15.7993
21.6300 0.0171
21.7600 0.0065
21.7900 0.0034
21.8500 3.9724
21.9500 0.0041
22.2000 0.0116
22.4000 0.0025
22.4300 1.1909
I need that in order to plot the values in bar where x will appear in categories 0-3.5, 3.5-6 etc.
Any help would be appreciated! Thank you very much!

Accepted Answer

Star Strider
Star Strider on 2 Jun 2014
Use the histc function.
I named your array xy and your bin edges binrng:
binrng = [0;3.5;6;8;10;12;14;16;18;20;22;24;26;28;30;32;36;40];
xy = [0 0.0040
17.8200 0.0030
19.0700 0.0035
19.1600 0.0028
19.9300 0.0035
. . . ];
counts = histc(xy(:,1),binrng)
  6 Comments
nath
nath on 4 Jun 2014
you are right, it is not possible, i will probably have to sum the values of y for each bin.
thanks anyway!!
Star Strider
Star Strider on 4 Jun 2014
My pleasure!
The bin distributions for y are referred to with the idx vector that histc returns. They will therefore have the same bin counts as x because you are using x to categorise them. If you bin the y values using your current bin assignments, all will be assigned to the first bin, except for one (15.8) that will be assigned to the eighth.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!