how to find which bin the number goes to in histogram
26 views (last 30 days)
Show older comments
Dear everybody, So, I have the following [h, value] = hist(X, 25); I then have a number coming in say Y=5. I would like to know which is the best "bin" to put it in. Note I do not want to update histogram. Just find where it belongs by returning the index to h. I know you could loop. But perhaps other more efficient way to do that? Thanks!
0 Comments
Answers (1)
Dr. Seis
on 9 Dec 2011
If you have:
X = rand(1,1000);
nbins = 25;
[n,xout] = hist(X,nbins);
Then "xout" will give you the center of the bin locations. The difference between, say, xout(2) and xout(1) will give you the spacing between each bin:
bin_spacing = xout(2)-xout(1);
If you have new value:
Y = rand(1);
then the simplest way to find the "best" bin would be to:
[min_val, index] = min(abs(xout-Y));
temp_n = zeros(size(n)); temp_n(index) = 1;
n = n + temp_n;
However, this assumes that "Y" will actually fall within a bin +/- bin_spacing/2. If you want to "throw away" the new value "Y" should it fall outside of any bin, then you will have to set up something to check this. If this is not important, then the above will simply put the new "Y" in the closest bin.
0 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!