www.gusucode.com > stats 源码程序 matlab案例代码 > stats/MeasureMahalanobisDistancesInGaussianMixtureDataExample.m

    %% Measure Mahalanobis Distances in Gaussian Mixture Data
%%
% Generate data from a mixture of two bivariate Gaussian distributions
% using the |mvnrnd| function.

% Copyright 2015 The MathWorks, Inc.

MU1 = [1 2];
SIGMA1 = [2 0; 0 .5];
MU2 = [-3 -5];
SIGMA2 = [1 0; 0 1];
rng(1); % For reproducibility
X = [mvnrnd(MU1,SIGMA1,1000);mvnrnd(MU2,SIGMA2,1000)];

scatter(X(:,1),X(:,2),10,'.')
hold on
%%
% Fit a two-component Gaussian mixture model.
obj = fitgmdist(X,2);
h = ezcontour(@(x,y)pdf(obj,[x y]),[-8 6],[-8 6]);
%%
% Compute the Mahalanobis distance of each point in |X| to the mean of each
% component of |obj|.
D = mahal(obj,X);

delete(h)
scatter(X(:,1),X(:,2),10,D(:,1),'.')
hb = colorbar;
ylabel(hb,'Mahalanobis Distance to Component 1')