Is there any way to scale histogram elements so instead of simple count get weighted sum within bins?
2 views (last 30 days)
Show older comments
Let's say I have an array of values: xy=[11,12,13,14,15,11] and an array of bins xyuni=[11,12,13,14,15] and an array of intensities int=[1,1,1,1,1,0.5].
What I would like to calculate is a histogram of xy values within the bins xyuni scaled by the intensities int, so the output I would like to get is:
[1.5,1,1,1,1].
Is there a simple way of doing this? Using histc, I can get the uniform-weighted histogram and bin indices from:
[hstres,binindices] = histc(xy,xyuni)
which gives
histres=[2,1,1,1,1] binindices=[1,2,3,4,5,1]
but I can't think of an easy way to scale by intensity. In reality I have really big arrays so am hoping to avoid a loop (the simple way would be to just loop over bin indices and sum/scale by intensity within the loop to calculate the weighted histogram that way but that would take too long with my really long arrays).
Any suggestions for a vectorized solution?
1 Comment
Jemima Puddleduck
on 20 Jun 2016
Edited: Jemima Puddleduck
on 20 Jun 2016
I would like to know the answer to this question too!!!
https://uk.mathworks.com/matlabcentral/answers/291105-how-to-make-histogram-with-volume-instead-of-frequency
Answers (2)
Azzi Abdelmalek
on 5 Dec 2014
Edited: Azzi Abdelmalek
on 5 Dec 2014
Edit
xy=[11.1,12.3,13.2,14.2,15.5,11.8]
xyuni=[11,12,13,14,15,16]
int=[1,1,1,1,1,0.5]
[a,b]=histc(xy,xyuni)
c=accumarray(b',int')
out=[c;zeros(numel(a)-numel(c),1)]
3 Comments
topolowa
on 1 Sep 2016
Hi Azzi, thanks for your coment. How would you do it if you want to bin X and Y coordinates scaled using Z values. X, Y, Z are vectors. thanks a lot!
andrepiz
on 7 Feb 2024
The call would be:
[bins, counts, edges] = histweight(xy, int)
Note that it is not vectorized and bins must be uniformly spaced.
Example for random points attached.
Disclaimer: I am the author of the toolbox.
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!