Is there any way to scale histogram elements so instead of simple count get weighted sum within bins?

12 次查看(过去 30 天)
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 个评论
Jemima Puddleduck
Jemima Puddleduck 2016-6-20
编辑:Jemima Puddleduck 2016-6-20
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

请先登录,再进行评论。

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-12-5
编辑:Azzi Abdelmalek 2014-12-5
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 个评论
topolowa
topolowa 2016-9-1
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
andrepiz 2024-2-7
you can use histweight available on MATLAB exchange.
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.

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by