Group data in bins
조회 수: 38 (최근 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에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!