Daniel@0: function x = gsamp(mu, covar, nsamp) Daniel@0: %GSAMP Sample from a Gaussian distribution. Daniel@0: % Daniel@0: % Description Daniel@0: % Daniel@0: % X = GSAMP(MU, COVAR, NSAMP) generates a sample of size NSAMP from a Daniel@0: % D-dimensional Gaussian distribution. The Gaussian density has mean Daniel@0: % vector MU and covariance matrix COVAR, and the matrix X has NSAMP Daniel@0: % rows in which each row represents a D-dimensional sample vector. Daniel@0: % Daniel@0: % See also Daniel@0: % GAUSS, DEMGAUSS Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: d = size(covar, 1); Daniel@0: Daniel@0: mu = reshape(mu, 1, d); % Ensure that mu is a row vector Daniel@0: Daniel@0: [evec, eval] = eig(covar); Daniel@0: Daniel@0: deig=diag(eval); Daniel@0: Daniel@0: if (~isreal(deig)) | any(deig<0), Daniel@0: warning('Covariance Matrix is not OK, redefined to be positive definite'); Daniel@0: eval=abs(eval); Daniel@0: end Daniel@0: Daniel@0: coeffs = randn(nsamp, d)*sqrt(eval); Daniel@0: Daniel@0: x = ones(nsamp, 1)*mu + coeffs*evec';