annotate toolboxes/FullBNT-1.0.7/KPMstats/mixgauss_sample.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [data, indices] = mixgauss_sample(mu, Sigma, mixweights, Nsamples)
wolffd@0 2 % mixgauss_sample Sample data from a mixture of Gaussians
wolffd@0 3 % function [data, indices] = mixgauss_sample(mu, Sigma, mixweights, Nsamples)
wolffd@0 4 %
wolffd@0 5 % Model is P(X) = sum_k mixweights(k) N(X; mu(:,k), Sigma(:,:,k)) or Sigma(k) for scalar
wolffd@0 6 % data(:,i) is the i'th sample from P(X)
wolffd@0 7 % indices(i) is the component from which sample i was drawn
wolffd@0 8
wolffd@0 9 [D K] = size(mu);
wolffd@0 10 data = zeros(D, Nsamples);
wolffd@0 11 indices = sample_discrete(mixweights, 1, Nsamples);
wolffd@0 12 for k=1:K
wolffd@0 13 if ndims(Sigma) < 3
wolffd@0 14 sig = Sigma(k);
wolffd@0 15 else
wolffd@0 16 sig = Sigma(:,:,k);
wolffd@0 17 end
wolffd@0 18 ndx = find(indices==k);
wolffd@0 19 if length(ndx) > 0
wolffd@0 20 data(:,ndx) = sample_gaussian(mu(:,k), sig, length(ndx))';
wolffd@0 21 end
wolffd@0 22 end