Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在MATLAB中绕数据椭圆

我想在MATLAB中重现下图:



有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)) 仅供参考, 是关于如何绘制椭圆的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] [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

编辑

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

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);



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 20:48


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