The associated histc function does this, but this requires defining the edges of the bin instead of the bin centers.
Y = rand(1, 10); edges = .1:.1:1; [N, I] = histc(Y, edges);
Calculation of edges, given that binzenters are also easy. In one insert:
N = hist(Y, X);
becomes
[Nc, Ic] = histc(Y, [-inf X(1:end-1) + diff(X)/2, inf]);
with Nc == N, plus one extra blank bit at the end (since I don't assume that the value in Y matches inf). See doc histc .
source share