mogp.m

来自「模式识别工具箱,希望对大家有用!」· M 代码 · 共 50 行

M
50
字号
function p = mogP(x,means,covs,priors)% p = mogP(x,means,covs,priors)%% Calculate the probability density for each of the clusters of a% mixture of Gaussians (note that P is not normalized!)% Copyright: D. Tax, R.P.W. Duin, davidt@ph.tn.tudelft.nl% Faculty of Applied Physics, Delft University of Technology% P.O. Box 5046, 2600 GA Delft, The Netherlands[N,d] = size(x);k = length(priors);p = zeros(N,k);covtype = ndims(covs);if ((covtype==2)&(size(covs,2)==1))  covtype = 1;endswitch covtype  case 1    D = distm(x,means);    sig = ones(N,1)*(2.*covs');    Z = (pi*sig).^(d/2);    p = exp(-(D./sig))./Z;  case 2    Z = (2*pi).^(d/2);    sig = prod(sqrt(covs),2);    for i=1:k      dif = x - ones(N,1)*means(i,:);      p(:,i) = exp(-sum((dif.*dif)./(ones(N,1)*covs(i,:)) ,2)/2) ./ ...                   (Z*sig(i));    end  case 3    Z = (2*pi).^(d/2);    for i=1:k      dif = x - ones(N,1)*means(i,:);      c = chol(squeeze(covs(i,:,:)));      Dmah = dif/c;      p(:,i) = exp(-sum(Dmah.*Dmah,2)/2) ./ (Z*prod(diag(c)));    end  otherwise   error('The covariance matrix parameter is not well-defined');end% include the priors:p = p.*(ones(N,1)*priors);return

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?