how to find which bin the number goes to in histogram
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Dr. Seis
2011-12-9
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!