How to construct a weighed 2D histogram?

25 ビュー (過去 30 日間)
Antranik Sefilian
Antranik Sefilian 2019 年 7 月 11 日
編集済み: andrepiz 2024 年 2 月 7 日
Hey,
I am finding it hard to construct a weighed 2D histogram.
Let us assume that we have two vectors, X and Y, of equal length N such that the entries in each of X and Y are not necessarily unique. Similarly, let's assume that W represents an array of N non-unique, non-integer values weighing each sample point [ X(i), Y(i) ].
To construct a standard 2D histogram, I am aware one can simply use
H = histogram2( X, Y )
to plot (along the z-axis) the sum/count of number of points per bin.
My question: how can I modify this to account for the weight of every sample point within a bin? That is, for the result to be the sum of the weights of all points within a bin.
(Note that this is not equivalent to merely weighing the count per bin by a constant value)
Thanks for any suggestions!

回答 (2 件)

John D'Errico
John D'Errico 2019 年 7 月 12 日
Just use accumarray to sum the weights in each bin.
  3 件のコメント
John D'Errico
John D'Errico 2019 年 7 月 12 日
Did you read the help for accumarray? Why not? Note that there are 8 examples of use of accumarray, here:
help accumarray
There will be more complete examples and explanations in
doc accumarray
READ THE HELP!
You will find that you first need to use discretize to bin each sample, so do that twice, once for each dimension. But again, you will find examples in the help. I cannot do better than to re-write the help, and how will that gain you or me?
help discretize
Antranik Sefilian
Antranik Sefilian 2019 年 7 月 27 日
編集済み: Antranik Sefilian 2019 年 7 月 27 日
Hey John,
Thanks for referring me to accumarray. I now have a piece of code which serves my needs partially; that is, without weighing individual points [x(i), y(i)].
% Create random data
x = randn(1e3,1); y = randn(1e3,1)*2 ;
% Centers of bins (I use integers)
xbins = floor(min(x)):1:ceil(max(x)); NumBins_x = numel(xbins);
ybins = floor(min(y)):1:ceil(max(y)); NumBins_y = numel(ybins);
% Mapping data to bin indices
Xi = round( interp1(xbins, 1:NumBins_x, x, 'linear', 'extrap') );
Yi = round( interp1(ybins, 1:NumBins_y, y, 'linear', 'extrap') );
% Limit indices to the range [1, NumBins]
Xi = max( min(Xi,NumBins_x), 1);
Yi = max( min(Yi,NumBins_y), 1);
% Use accumarray to count number of points/bin
H = accumarray([Yi(:) Xi(:)], 1, [NumBins_y NumBins_x]);
% Plotting
[XX, YY] = meshgrid(xbins, ybins);
figure(1); surf(XX, YY, H);
shading interp; colorbar; colormap jet; view(0,90);
% hold on; plot(x,y, 'r.', 'MarkerSize',10) ; To visualize underlying data
Update: I gather that I can sum the weights (rather than the number of points) in each bin by the following:
w = randn(1e3, 1) ; % Define weights: same size as [x] & [y]
H = accumarray([Yi(:) Xi(:)], w, [NumBins_y NumBins_x]); %i.e. vals = w, not 1
I'd appreciate it if you can provide your feedback on this.

サインインしてコメントする。


andrepiz
andrepiz 2024 年 2 月 7 日
編集済み: andrepiz 2024 年 2 月 7 日
you can use histweight available on MATLAB exchange.
The call would be:
[bins, counts, edges] = histweight([X;Y], W)
Example for random points attached.
Disclaimer: I am the author of the toolbox.

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by