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.
Best Answer