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

    %% Fit a Gaussian Mixture Model  
% Generate data from a mixture of two bivariate Gaussian distributions using
% the |mvnrnd| function. Fit the resulting data.   

%% 
% Generate the data using 1000 points from each distribution. 
rng(1); % For reproducibility 
MU1 = [1 2];
SIGMA1 = [2 0; 0 .5];
MU2 = [-3 -5];
SIGMA2 = [1 0; 0 1];
X = [mvnrnd(MU1,SIGMA1,1000);mvnrnd(MU2,SIGMA2,1000)];

scatter(X(:,1),X(:,2),10,'.')
hold on     

%% 
% Fit a two-component Gaussian mixture model. 
options = statset('Display','final');
obj = fitgmdist(X,2,'Options',options);  

%% 
% Plot the fit. 
h = ezcontour(@(x,y)pdf(obj,[x y]),[-8 6],[-8 6]);