File Exchange

image thumbnail

PLOT_GAUSSIAN_ELLIP​SOID

version 1.0.0.1 (2.6 KB) by

Plots two- and three-dimensional Gaussian distributions

3.92857
14 Ratings

23 Downloads

Updated

View License

PLOT_GAUSSIAN_ELLIPSOIDS plots 2-d and 3-d Gaussian distributions.

H = PLOT_GAUSSIAN_ELLIPSOIDS(M, C) plots the distribution specified by mean M and covariance C. The distribution is plotted as an ellipse (in 2-d) or an ellipsoid (in 3-d). By default, the distributions are plotted in the current axes. H is the graphics handle to the plotted ellipse or ellipsoid.

PLOT_GAUSSIAN_ELLIPSOIDS(M, C, SD) uses SD as the standard deviation along the major and minor axes (larger SD => larger ellipse). By default, SD = 1.

PLOT_GAUSSIAN_ELLIPSOIDS(M, C, SD, NPTS) plots the ellipse or ellipsoid with a resolution of NPTS (ellipsoids are generated on an NPTS x NPTS mesh). By default, NPTS = 50 for ellipses, and 20 for ellipsoids.

PLOT_GAUSSIAN_ELLIPSOIDS(M, C, SD, NPTS, AX) adds the plot to the axes specified by the axis handle AX.

Comments and Ratings (17)

Dima

Dima (view profile)

Xiangping Zhu

Yoshio

Yoshio (view profile)

This is a great function! It might be possible to use gammaincinv for swidth calculation inside the function.

sqrt(2*gammaincinv(p,D/2)), instead of sqrt(chi2inv(p,D))

D = [ 1 2 3];
>> p
p =
0.9500
>> swidth = sqrt(2*gammaincinv(p,D/2))
swidth =
1.9600 2.4477 2.7955
>> sqrt(chi2inv(p,D))
ans =
1.9600 2.4477 2.7955

I have a small consideration on swidth.
It could be a better idea to input not a swidth but the fraction of population that I want to include into the ellipse/ellipsoid (i.e. p=0.95 swidth=1.96 for gaussian univariate distribution).
So for the ellipse, sdwidth will be
swidth=sqrt(chi2inv(p,2));
and for the ellipsoid
swidth=sqrt(chi2inv(p,3));

This has been an excellent visualization tool.

But I want to make sure it's doing exactly what I think it's doing--does this function plot every gaussian at the border of its first standard deviation?

Please, never mind my previous comment. It is completely all right in the code. Sorry about the confusion.

There seems to be a small error with large consequences in the computation of the ellipsoids.

Basically, when you use the matrix of eigenvectors (v) and eigenvalues (d) to perform the rotation of ap as (v*d*ap), you should be using the square root of the eigenvalues. That is, the rotation should be (v*sqrt(d)*ap).

You can convince yourself that there is something wrong if the square root is not applied by computing the Mahalanobis distance from the mean to the points of the ellipsoid. This distance must be constant for all the points of the ellipsoid, and it is not unless you take the square root.

Thanks for your sharing your function!

Sultan Alzahrani

Great job... I used it in my project and gave you a credit!

jon erickson

jon erickson (view profile)

works great and excellent documentation. thanks!

Alex

Alex (view profile)

Thanks for this one!! Works great.

Fejjro

Fejjro (view profile)

Simply spectacular! Using a sphere is a good idea.

J Ugander

gca problem fixed, does what it says, well.

John D'Errico

There is no need to move the help. It is in an entirely appropriate place.

I'll add a couple of minor comments. A standard style in matlab for functions that return graphics handles is not to return the handle if no output argument is supplied. See any function like plot, surf, etc. So why not check to see if there are any output arguments requested? If there are none, then do not return h. Thus add this test at the end of your file:

if nargout == 0
clear h
end

It was also unclear what the "number of points" means in the ellipsoid case. Is this the number of longitudinal lines?

Good help otherwise, good error checks, H1 line, examples, etc.

Gautam Vallabha

"Put the help above the function, otherwise help cannot be displayed.". The help location in the m-file (below the function definition line) is a MathWorks convention. In any case, 'help plot_gaussian_ellipsoid' should work for all recent versions of MATLAB.

Ram Acharya

Put the help above the function, otherwise help cannot be displayed.

Updates

1.0.0.1

Updated license

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video