MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中绕数据椭圆 (https://www.labfans.com/bbs/showthread.php?t=23761)

poster 2019-12-10 20:48

在MATLAB中绕数据椭圆
 
我想在MATLAB中重现下图:

[IMG]https://i.stack.imgur.com/4tegY.png[/IMG]

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

该图是使用其他软件创建的,我不完全了解它如何计算椭圆。

这是我用于此图的数据。第一列是class,第二列-X,第三列gscatter 。我可以使用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)) 仅供参考, [URL="https://stackoverflow.com/questions/2153768/matlab-to-draw-ellipse-and-ellipsoid"]这[/URL]是关于如何绘制椭圆的SO问题。因此,我们只需要知道绘制它的所有参数即可。

[B]更新:[/B]

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



[B]回答:[/B]

考虑一下代码:

%# 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] [VD] = 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 [IMG]https://i.stack.imgur.com/oXnYX.png[/IMG]

[B]编辑[/B]

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

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; [VD] = eig(Cov); [IMG]https://i.stack.imgur.com/GzR0J.png[/IMG]



[url=https://stackoverflow.com/questions/3417028]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:08

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.