MATLAB: Extract values within a range in a for loop

findfor loophistogramindexingiterationMATLAB

I have a loop like this:
for i = 1:length(x)
xy = find(u==i | u>i & u<(i+1)); #xy should find indices of u within that range i.e u(i)>=i<u(i+1)
N(i) = length(xy) #giving me counts of i
end
but then, the last iteration (last value of i) will be wrong by this expression.
how do i correct this please?
Or any better suggestion (function) by which I can do this
Thanks a lot

Best Answer

histcounts or the older histc.
Note that the two functions have different results for values that are exactly equal to the last element of the vector of bins. histc uses a separate bin for values exactly equal to the last bin but histc makes it part of the previous bin.