Group data in bins
101 次查看(过去 30 天)
显示 更早的评论
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 个评论
Paul Hoffrichter
2021-2-24
If you provide a small set of input data, and show the desired output, members should be able to help you better.
回答(4 个)
Cris LaPierre
2021-2-21
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.
0 个评论
Paul Hoffrichter
2021-2-21
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
0 个评论
Steven Lord
2021-2-21
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.
Cris LaPierre
2021-2-22
In that case, i would look into grpstats or groupsummary. You might also be interested in using findgroups in conjunction with splitapply.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!