Daniel@0: function M = sample_gaussian(mu, Sigma, N) Daniel@0: % SAMPLE_GAUSSIAN Draw N random row vectors from a Gaussian distribution Daniel@0: % samples = sample_gaussian(mean, cov, N) Daniel@0: Daniel@0: if nargin==2 Daniel@0: N = 1; Daniel@0: end Daniel@0: Daniel@0: % If Y = CX, Var(Y) = C Var(X) C'. Daniel@0: % So if Var(X)=I, and we want Var(Y)=Sigma, we need to find C. s.t. Sigma = C C'. Daniel@0: % Since Sigma is psd, we have Sigma = U D U' = (U D^0.5) (D'^0.5 U'). Daniel@0: Daniel@0: mu = mu(:); Daniel@0: n=length(mu); Daniel@0: [U,D,V] = svd(Sigma); Daniel@0: M = randn(n,N); Daniel@0: M = (U*sqrt(D))*M + mu*ones(1,N); % transform each column Daniel@0: M = M'; Daniel@0: