Ellipse around the data in MATLAB

后端 未结 3 582
太阳男子
太阳男子 2020-12-08 16:56

I would like to reproduce the following figure in MATLAB:

\"exampleee.png\"

There are two classes of po

相关标签:
3条回答
  • 2020-12-08 17:28

    I'll assume there is only one set of points given in a single matrix, e.g.

    B = A(1:10,2:3);
    

    you can reproduce this procedure for each data set.

    1. Compute the center of the ellipsoid, which is the mean of the points. Matlab function: mean
    2. Center your data. Matlab function bsxfun
    3. Compute the principal axis of the ellipsoid and their respective magnitude. Matlab function: eig

    The successive steps are illustrated below:

    Center = mean(B,1);
    Centered_data = bsxfun(@minus,B,Center);
    [AX,MAG] = eig(Centered_data' * Centered_data);
    

    The columns of AX contain the vectors describing the principal axis of the ellipsoid while the diagonal of MAG contains information on their magnitude. To plot the ellipsoid, scale each principal axis with the square root of its magnitude.

    Hope this helps.

    A.

    0 讨论(0)
  • 2020-12-08 17:32

    Consider the code:

    %# 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


    EDIT

    If you want the ellipse to represent a specific level of standard deviation, the correct way of doing is by scaling the covariance matrix:

    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

    0 讨论(0)
  • 2020-12-08 17:33

    I'd try the following approach:

    1. Calculate the x-y centroid for the center of the ellipse (x,y in the linked question)
    2. Calculate the linear regression fit line to get the orientation of the ellipse's major axis (angle)
    3. Calculate the standard deviation in the x and y axes
    4. Translate the x-y standard deviations so they're orthogonal to the fit line (a,b)
    0 讨论(0)
提交回复
热议问题