我正在使用 Matlab 2022a 来解决这个问题,但如果有人有通用方程/算法或另一个程序(例如 R)中的良好解决方案,我也有兴趣了解这一点。
我有一组空间数据(生成并绘制在下面的模拟数据),每个位置都有 x、y 和第三列数据值。
num = 100;
P = mvnrnd([0,0,20], [ 11.3669 -2.9268 -2.0568;
-2.9268 1.3372 0.4483;
-2.0568 0.4483 51.9697],num);
CenterofMass=sum(P(:,1:2).*P(:,3))/sum(P(:,3)); %weighted mean
scatter(P(:,1),P(:,2),abs(P(:,3))), hold on
plot(CenterofMass(:,1),CenterofMass(:,2),'r+')
我已经看过this讨论,但它不涉及在位置处拥有数据值。 计算质心相对容易,但我正在努力解决如何处理具有权重的数据的空间协方差(无论需要什么等价物)。 感谢您的任何想法! 丽贝卡
这个想法是调整您的数据,以便您可以使用您链接到的帖子中的出色答案。
您可以通过缩放点的数量来创建一组具有相同质量的点,而不是拥有一组具有不同质量的点:
num = 100;
P = mvnrnd([0,0,20], [ 11.3669 -2.9268 -2.0568;
-2.9268 1.3372 0.4483;
-2.0568 0.4483 51.9697],num);
CenterofMass=sum(P(:,1:2).*P(:,3))/sum(P(:,3)); %weighted mean
scatter(P(:,1),P(:,2),abs(P(:,3))), hold on
plot(CenterofMass(:,1),CenterofMass(:,2),'r+')
% Create array X
% digits kept (mass is not an integer)
prec = 2;
nX = round(10^prec*P(:,3));
X = [];
for ii=1:size(P,1)
% Each point is repeated nX(ii) times to get correct mass
X = [X;[P(ii,1),P(ii,2)].*ones(nX(ii),1)];
end
现在,您可以使用answer中的代码,也可以使用编辑部分:
Mu = mean( X );
X0 = bsxfun(@minus, X, Mu);
STD = 2; %# 2 standard deviations
conf = 2*normcdf(STD)-1; %# covers around 95% of population
% --> In the tweaked dataset X, this is equivalent to 95% of the mass
scale = chi2inv(conf,2);
%# eigen decomposition [sorted by eigen values]
[V,D] = eig(scale* (X0'*X0) ./ (size(X,1)-1) ); %#' cov(X0)
[D,order] = sort(diag(D), 'descend');
D = diag(D);
V = V(:, order);
t = linspace(0,2*pi,100);
e = [cos(t) ; sin(t)]; %# unit circle
VV = V*sqrt(D); %# scale eigenvectors
e = bsxfun(@plus, VV*e, Mu'); %#' project circle back to orig space
%# plot cov and major/minor axes
plot(e(1,:), e(2,:), 'Color','k');