sum values of histogram bins
52 次查看(过去 30 天)
显示 更早的评论
Hi, I have data on particle intensities. I am binning the particles into bins based upon the intensities. This code is working.
histBins=[10, 25, 50, 75, 100, 150, 200];
a=hist(d,histBins);
I will now like to sum up the intensity of all the particles in each bin. Is there a way to do this?
Thanks, John
0 个评论
采纳的回答
Sean de Wolski
2014-5-28
Building on The Cyclist's answer, use accumarray to do the summing:
x = randn(100,1);
edges = -10:10;
[~, idx] = histc(x,edges);
binsums = accumarray(idx,x)
0 个评论
更多回答(2 个)
Image Analyst
2014-5-28
编辑:Image Analyst
2014-5-28
No need to mess with histograms, and resulting loss of precision. If you want "sum up the intensity of all the particles", just sum the particles themselves, not the histogram bins.
sumOfAllParticleIntensities = sum(d);
If you want the sums of all particle intensities bin by bin between the bin edges that you gave, then you can do this:
histBins=[10, 25, 50, 75, 100, 150, 200];
x = 200*randn(100,1);
for k = 1 : length(histBins)-1
indexes = x > histBins(k) & x <= histBins(k+1);
binByBinSums(k) = sum(x(indexes));
end
% Print to command window:
binByBinSums
This will be more accurate than doing anything based on counts and the values of the bin centers.
0 个评论
the cyclist
2014-5-28
编辑:the cyclist
2014-5-28
If you change your code to use the histc() command instead of the hist() command, then you can do
[count,idx] = histc()
will give you the index to which bin each element was sorted into. You can use that index sum the intensities in each bin.
[histc() specifies the bins based on edges, rather than centers.]
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!