sum values of histogram bins
38 ビュー (過去 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!