Group data in bins
Ältere Kommentare anzeigen
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 Kommentare
KALYAN ACHARJYA
am 21 Feb. 2021
If you want help, you need to be more clear, so that members can help you.
Ivan Mich
am 21 Feb. 2021
Paul Hoffrichter
am 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.
Antworten (4)
Cris LaPierre
am 21 Feb. 2021
1 Stimme
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.
Paul Hoffrichter
am 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
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)
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 Kommentar
Ivan Mich
am 22 Feb. 2021
Cris LaPierre
am 22 Feb. 2021
0 Stimmen
In that case, i would look into grpstats or groupsummary. You might also be interested in using findgroups in conjunction with splitapply.
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!