View License

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

» Watch video

Highlights from

4.5 | 12 ratings Rate this file 52 Downloads (last 30 days) File Size: 2.6 KB File ID: #16543 Version:
image thumbnail




23 Sep 2007 (Updated )

Plots two- and three-dimensional Gaussian distributions

| Watch this File

File Information

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.


This file inspired Expectation Maximization Algorithm.Zip and Condensation Of Earthquake Location Distributions: Optimal Spatial Information Encoding.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (15)
05 Jan 2017 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 =
>> 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

05 Jan 2017 Daniel Adolfsson

09 Sep 2016 Giuseppe Cardillo

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
and for the ellipsoid

Comment only
17 Jun 2016 Christopher Menart

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?

Comment only
03 Jul 2014 Fernando Iglesias

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

02 Jul 2014 Fernando Iglesias

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!

13 Apr 2014 Sultan Alzahrani

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

10 Jan 2014 jon erickson

jon erickson (view profile)

works great and excellent documentation. thanks!

03 Jun 2011 Alex

Alex (view profile)

Thanks for this one!! Works great.

02 Apr 2009 Fejjro

Fejjro (view profile)

Simply spectacular! Using a sphere is a good idea.

26 Feb 2009 Andrew Callender

12 Oct 2007 J Ugander

gca problem fixed, does what it says, well.

24 Sep 2007 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

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.

24 Sep 2007 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.

Comment only
24 Sep 2007 Ram Acharya

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

01 Sep 2016

Updated license

Contact us