% random numbers following a multivariate Gaussian with mean vector mu and covariance matrix C, considering correlations and potential singularities in C ret = rand_gauss(mu, C, n) mu = double dx1, mean vector of dimension d C = double dxd, covariance matrix n = int, number of samples to generate ret = double dxn, n samples of Gaussian(Mu,C) author: Richard Steffen
0001 %% random numbers following a multivariate Gaussian with 0002 % mean vector mu and covariance matrix C, considering correlations and 0003 % potential singularities in C 0004 % 0005 % ret = rand_gauss(mu, C, n) 0006 % 0007 % mu = double dx1, mean vector of dimension d 0008 % C = double dxd, covariance matrix 0009 % n = int, number of samples to generate 0010 % ret = double dxn, n samples of Gaussian(Mu,C) 0011 % 0012 % author: Richard Steffen 0013 0014 function ret = rand_gauss(mu, C, n) 0015 0016 % Zerlegung in eigenvectoren und Eigenwerte 0017 [R,S] = eig(full(C)); 0018 % Eigenvektoren skalieren mit Eigenwerten 0019 A = R .* repmat(sqrt(abs(diag(S)))',length(S),1); 0020 ret = repmat(mu,1,n) + A*randn(length(mu),n); 0021