Group data in bins

135 views (last 30 days)
Ivan Mich
Ivan Mich on 21 Feb 2021
Commented: Paul Hoffrichter on 24 Feb 2021
I have a file with data numbers. I would like to group them , let's say in "bins", by a specific step (eg minimum value is 0, maximum is 20. So I would like to make bins by 0.5 step and make groups with these values).
I think this:
% Define the bin edges you want
EDGES = 0:0.5:20;
% Bin the data according to the predefined edges:
Y = histcounts(x, EDGES);
But the point is that I would like to take the y values that correspont to these bins.
Could you help me please?
  3 Comments
Ivan Mich
Ivan Mich on 21 Feb 2021
ok, I have one input file with data (x,y). I want to group my data as I mentioned above (I did this). I would like to have the y values that correspond to each bin.
How could I do this?
Paul Hoffrichter
Paul Hoffrichter on 24 Feb 2021
If you provide a small set of input data, and show the desired output, members should be able to help you better.

Sign in to comment.

Answers (4)

Cris LaPierre
Cris LaPierre on 21 Feb 2021
The first output of histcounts is the count of items in each bin.
You could use this syntax to at least determine which bin each X value was assigned to. You could probably use that to then group the y values, too.
You might want to look into the findgroups function.

Paul Hoffrichter
Paul Hoffrichter on 21 Feb 2021
Hope this is close to what you are looking for.
EDGES'
ans =
0
0.5000
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000 <- Bin 8 covers [3.5 to 4.0)
4.0000
4.5000
...
19.0000
19.5000
20.0000
X = abs( randn(16,1)*5 );
X =
0.1739
3.9908 <- Bin 8 in [3.5 to 4.0)
5.0934
0.6661
3.5727 <- Bin 8 in [3.5 to 4.0)
6.7569
1.1239
2.9451
1.4688
4.2396
5.6006
12.6300
[~, ~, bin] = histcounts(X, EDGES);
bin_X = [bin X]
1.0000 0.1739
8.0000 3.9908 <- Bin 8 in [3.5 to 4.0)
11.0000 5.0934
2.0000 0.6661
8.0000 3.5727 <- Bin 8 in [3.5 to 4.0)
14.0000 6.7569
3.0000 1.1239
6.0000 2.9451
3.0000 1.4688
9.0000 4.2396
12.0000 5.6006
26.0000 12.6300
X_bin_sorted = sort(bin_X)
1.0000 0.1739
2.0000 0.6661
3.0000 1.1239
3.0000 1.4688
6.0000 2.9451
8.0000 3.5727 <- Bin 8 in [3.5 to 4.0)
8.0000 3.9908 <- Bin 8 in [3.5 to 4.0)
9.0000 4.2396
11.0000 5.0934
12.0000 5.6006
14.0000 6.7569
26.0000 12.6300

Steven Lord
Steven Lord on 21 Feb 2021
If you just need to know in which bin each element falls, use discretize.
E = 0:11;
x = 10*rand(20, 1);
bin = discretize(x, E);
result = table(x, bin)
result = 20x2 table
x bin _______ ___ 4.2765 5 9.6732 10 9.5604 10 3.2379 4 8.338 9 8.4339 9 7.7412 8 2.1064 3 0.67424 1 0.50379 1 2.7735 3 3.0882 4 5.5501 6 9.5552 10 3.2904 4 1.7843 2
If your data could take on the values 9 and 10 and your edges vector was 0:10 the last bin would contain both those elements with value 9 and those with value 10. The last bin contains both its left edge and its right edge, while the other bins contain their left but not their right. That why the edges vector goes to 11.
  1 Comment
Ivan Mich
Ivan Mich on 22 Feb 2021
I want to know exactly which numbers are in each bin so that I can do statistical calculations (for example mean and standard deviations for each class) that I want.
Do you know how?

Sign in to comment.


Cris LaPierre
Cris LaPierre on 22 Feb 2021
In that case, i would look into grpstats or groupsummary. You might also be interested in using findgroups in conjunction with splitapply.

Categories

Find more on Contour Plots 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!