Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ellipse around the data in MATLAB

I would like to reproduce the following figure in MATLAB:

exampleee.png

There are two classes of points with X and Y coordinates. I'd like to surround each class with an ellipse with one parameter of standard deviation, which determine how far the ellipse will go along the axis.

The figure was created with another software and I don't exactly understand how it calculates the ellipse.

Here is the data I'm using for this figure. The 1st column is class, 2nd - X, 3rd - Y. I can use gscatter to draw the points itself.

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

FYI, here is the SO question on how to draw ellipse. So, we just need to know all the parameters to draw it.


Update:

I agree that the center can be calculated as the means of X and Y coordinates. Probably I have to use principal component analysis (PRINCOMP) for each class to determine the angle and shape. Still thinking...

like image 395
yuk Avatar asked Aug 05 '10 16:08

yuk


People also ask

How do you fit data in an ellipse in MATLAB?

Accepted Answer x = a(1) + a(2)*cos(t); y = a(3) + a(4)*sin(t) ; Here, you are trying to find "a" to determine the best fit of x and y (given t) to these equations in the least-squares sense. (Assume you do not know where the ellipse is centered.

What does ellipse do MATLAB?

The ellipsis is the line continuation operator in MATLAB. Continue any MATLAB command or expression by placing an ellipsis at the end of the line to be continued: sprintf('The current value of %s is %d', ... vname, value)

How do you fit an ellipse into a picture?

Find the closest one with min. Then take the mean or sum of those closest distances. % Get distance of each actual boundary point to all points in your ellipse fit vectors. closestDistances(k) = min(distances); % A single distance.

What does =~ mean in MATLAB?

It means not equal to as you say. It doesn't have any other meaning in MATLAB. The ~ by itself has meaning. It can be used to not return certain outputs from a function.


1 Answers

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

like image 185
Amro Avatar answered Sep 16 '22 16:09

Amro