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.
Gautam Vallabha (2020). PLOT_GAUSSIAN_ELLIPSOID (https://www.mathworks.com/matlabcentral/fileexchange/16543-plot_gaussian_ellipsoid), MATLAB Central File Exchange. Retrieved .
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];
>> swidth = sqrt(2*gammaincinv(p,D/2))
1.9600 2.4477 2.7955
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
and for the ellipsoid
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!
Great job... I used it in my project and gave you a credit!
works great and excellent documentation. thanks!
Thanks for this one!! Works great.
Simply spectacular! Using a sphere is a good idea.
gca problem fixed, does what it says, well.
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
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.
"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.
Put the help above the function, otherwise help cannot be displayed.
Added nargout==0 check.