gpt4 book ai didi

matlab - MATLAB 中数据周围的椭圆

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:03 25 4
gpt4 key购买 nike

我想在 MATLAB 中重现下图:

exampleee.png

有两类具有 X 和 Y 坐标的点。我想用一个带有一个标准偏差参数的椭圆包围每个类,它确定椭圆沿轴的距离。

这个图形是用另一个软件创建的,我不太明白它是如何计算椭圆的。

这是我用于此图的数据。第一列是类,第二列 - X,第三列 - Y。我可以使用 gscatter 自己绘制点。

A = [
0 0.89287 1.54987
0 0.69933 1.81970
0 0.84022 1.28598
0 0.79523 1.16012
0 0.61266 1.12835
0 0.39950 0.37942
0 0.54807 1.66173
0 0.50882 1.43175
0 0.68840 1.58589
0 0.59572 1.29311
1 1.00787 1.09905
1 1.23724 0.98834
1 1.02175 0.67245
1 0.88458 0.36003
1 0.66582 1.22097
1 1.24408 0.59735
1 1.03421 0.88595
1 1.66279 0.84183
];

gscatter(A(:,2),A(:,3),A(:,1))

仅供引用,here是关于如何绘制椭圆的 SO 问题。所以,我们只需要知道绘制它的所有参数。


更新:

我同意可以将中心计算为 X 和 Y 坐标的平均值。可能我必须对每个类使用主成分分析 (PRINCOMP) 来确定角度和形状。还在想...

最佳答案

考虑代码:

%# generate data
num = 50;
X = [ mvnrnd([0.5 1.5], [0.025 0.03 ; 0.03 0.16], num) ; ...
mvnrnd([1 1], [0.09 -0.01 ; -0.01 0.08], num) ];
G = [1*ones(num,1) ; 2*ones(num,1)];

gscatter(X(:,1), X(:,2), G)
axis equal, hold on

for k=1:2
%# indices of points in this group
idx = ( G == k );

%# substract mean
Mu = mean( X(idx,:) );
X0 = bsxfun(@minus, X(idx,:), Mu);

%# eigen decomposition [sorted by eigen values]
[V D] = eig( X0'*X0 ./ (sum(idx)-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');
%#quiver(Mu(1),Mu(2), VV(1,1),VV(2,1), 'Color','k')
%#quiver(Mu(1),Mu(2), VV(1,2),VV(2,2), 'Color','k')
end

screenshot


编辑

如果您希望椭圆表示特定水平的标准差,正确的做法是缩放协方差矩阵:

STD = 2;                     %# 2 standard deviations
conf = 2*normcdf(STD)-1; %# covers around 95% of population
scale = chi2inv(conf,2); %# inverse chi-squared with dof=#dimensions

Cov = cov(X0) * scale;
[V D] = eig(Cov);

OP_DATA

关于matlab - MATLAB 中数据周围的椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3417028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com