equal elements in historgam bins

14 views (last 30 days)
According to the help menu:
Calculate four evenly spaced quantiles.
y = quantile(x,4)
I want 10 bins with equal elements in each bin I tried :
y=quantile(x,10)
then
[counts,Id] = histc(x,y)
I received
counts=[3131
3130
3130
3130
3131
3130
3130
3130
3131
0]
and
Id=[ 0
1
2
3
4
5
6
7
8
9]
The issue is that the groups are 10 (0 to 9) but 3130 x 10 is not equal to the vector size of 34443x1. I a missing something here. as the size of each bin should be around 3443 and not 3130. Kindly, could you please spot the error and divide the histogram to 10 bins with equal elements?
  1 Comment
Image Analyst
Image Analyst on 6 Aug 2015
34443 is not a multiple of 10 so how could all the bins have equal numbers in them?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Aug 2015
nbin = 10;
y = quantile(x,nbin-1);
[counts, Id] = histc([-inf; y(:); inf]);
quantile returns N entries part way through the data. If you look at the example quantile(x,4) and notice the material about the equivalent probabilities, you can see that argument N would be for dividing into N+1 equal pieces.
histc uses the values passed as being the edges, and any value less than the first provided edge is not counted, so to count from the lowest value, prefix the quantile result with either min(x) or with -inf.
For histc, the last bin is to be used only for values that exactly equal the last edge value; the last bin is not used for all x from the last value upwards. For symmetry with the possibility of using min(x) as the first edge value, you might be tempted to use max(x) as the last edge value. However that would mean that the last bin would count only values exactly equal to max(x), so instead of max(x) use +inf as the last edge. This does theoretically lead to the possibility of an unexpected match against inf, if the input just happens to contain inf, but if that concerns you, you could easily adjust for it.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!