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

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [data, indices] = mixgauss_sample(mu, Sigma, mixweights, Nsamples)
Daniel@0 2 % mixgauss_sample Sample data from a mixture of Gaussians
Daniel@0 3 % function [data, indices] = mixgauss_sample(mu, Sigma, mixweights, Nsamples)
Daniel@0 4 %
Daniel@0 5 % Model is P(X) = sum_k mixweights(k) N(X; mu(:,k), Sigma(:,:,k)) or Sigma(k) for scalar
Daniel@0 6 % data(:,i) is the i'th sample from P(X)
Daniel@0 7 % indices(i) is the component from which sample i was drawn
Daniel@0 8
Daniel@0 9 [D K] = size(mu);
Daniel@0 10 data = zeros(D, Nsamples);
Daniel@0 11 indices = sample_discrete(mixweights, 1, Nsamples);
Daniel@0 12 for k=1:K
Daniel@0 13 if ndims(Sigma) < 3
Daniel@0 14 sig = Sigma(k);
Daniel@0 15 else
Daniel@0 16 sig = Sigma(:,:,k);
Daniel@0 17 end
Daniel@0 18 ndx = find(indices==k);
Daniel@0 19 if length(ndx) > 0
Daniel@0 20 data(:,ndx) = sample_gaussian(mu(:,k), sig, length(ndx))';
Daniel@0 21 end
Daniel@0 22 end