如何创建加权二维直方图
问题描述:
加权一维直方图看起来非常直截了当。我怎样才能实现一个二维加权直方图给出一个权重矢量,w?如何创建加权二维直方图
MATLAB:
x = randn(4,1); //some random x input
y = randn(4,1); //some random y input
w = [10, 20, 30, 40]; //weight to be assigned to corresponding data point in the generated histogram, i.e. the pixel intensity value
figure;
H = histogram2(x,y); //Matlab built-in 2D histogram
我应该如何在上面的代码片断使用瓦特得到一个加权2D直方图功能。
* MATLAB代码是首选,但也可能接受Python。
答
我分享我的加权2D直方图,我从我的一些旧的代码了...
function f = hist2dw(x,y,weights,xedges,yedges)
%all inputs should be vectors
[~,xbin] = histc(x,xedges);
[~,ybin] = histc(y,yedges);
xbin(xbin==0) = inf;
ybin(ybin==0) = inf;
xnbin = numel(xedges);
ynbin = numel(yedges);
xy = xbin * ynbin + ybin;
[xyu,id] = unique(xy);
xyu(end) = []; % remove Inf bin
id(end) = [];
hstres = histc(xy,xyu);
hstres = hstres.*weights(id); % add the weights to the histogram
f(ynbin,xnbin)=0; % preallocate memory
f(xyu-ynbin) = hstres;
,这里是如何使用它的一个例子:
x = rand(1000,1);
y = rand(1000,1);
w=[ones(900,1) ; 5*ones(100,1) ];
edg=0:0.01:1;
imagesc(edg,edg,hist2dw(x(:),y(:),w(:),edg,edg));colorbar
嗨。例如,如果您有4个值对,那么为什么要使用64个NBINS呢?无论如何,解决方案并不像x = x。* w那么简单。 Y = Y *瓦特; ?这样,第一排得到更低的重量等。 –
不完全。 x,y应给出直方图变换后的投影坐标。该2D平面中每个数据点的实际像素值应取对应的w值。至于垃圾箱,我通过删除Nbin来简化了这个问题。 – nikk