binning histogram and obtaining the proportions

3 views (last 30 days)
jenka
jenka on 4 May 2017
Edited: dpb on 4 May 2017
Dear all, I have a quick question. I have histogram and now suppose a new observation comes in and I would like to know which bin in my histogram does it belong to. I then would like to return the proportion of that particular bin. The bin proportion corresponds to the number of observations falling into that bin divided by the total number. Thank you all in advance.

Answers (1)

dpb
dpb on 4 May 2017
Edited: dpb on 4 May 2017
Simplest is probably to just return the bin of the observation via histcounts or histc, whichever you're using with the same bin edges vector as created the original.
[~,~,bin]=histcounts(newX,oldEdgesVector);
Or, it's a lookup using the edges vector
bin=fix(interp1(oldEdgesVector,1:length(oldEdgesVector),newX));
Note interp1 will return interpolated value; return integer portion to get bin. 'nearest' will round, so don't want that. 'previous' may yield same result; I didn't test; I'd probably use the former to be sure had same internal logic w/o worrying about details.

Tags

Community Treasure Hunt

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

Start Hunting!