X=zeros(600,2); X(1:200,:) = normrnd(0,1,200,2); X(201:400,:) = normrnd(0,2,200,2); X(401:600,:) = normrnd(0,3,200,2); [W,M,V,L] = EM_GM(X,3,[],[],1,[]) 下面是程序源码: 打印帮助 function[W,M,V,L] = EM_GM(X,k,ltol,maxiter,pflag,Init) % [W,M,V,L] = EM_GM(X,k,ltol,maxiter,pflag,Init) % % EM algorithm for k multidimensional Gaussian mixture estimation % % Inputs: % X(n,d) - input data, n=number of observations, d=dimension of variable % k - maximum number of Gaussian components allowed % ltol - percentage of the log likelihood difference between 2 iterations ([] for none) % maxiter - maximum number of iteration allowed ([] for none) % pflag - 1 for plotting GM for 1D or 2D cases only, 0 otherwise ([] for none) % Init - structure of initial W, M, V: Init.W, Init.M, Init.V ([] for none) % % Ouputs: % W(1,k) - estimated weights of GM % M(d,k) - estimated mean vectors of GM % V(d,d,k) - estimated covariance matrices of GM % L - log likelihood of estimates % % Written by % Patrick P. C. Tsui, % PAMI research group % Department of Electrical and Computer Engineering % University of Waterloo, % March, 2006 % %%%% Validate inputs %%%% ifnargin <= 1, disp('EM_GM must have at least 2 inputs: X,k!/n') return elseifnargin == 2, ltol = 0.1; maxiter = 1000; pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); iferr_X | err_k,return;end elseifnargin == 3, maxiter = 1000; pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); iferr_X | err_k | err_ltol,return;end elseifnargin == 4, pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); [maxiter,err_maxiter] = Verify_maxiter(maxiter); iferr_X | err_k | err_ltol | err_m...