sum values of histogram bins
조회 수: 36 (최근 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!