function X = randnv(mu, cx, N) % % function X = randnv(mu, cx, N) % % this function generates the normal random vector with mean mu and covariance cx. % The function first generate the normal random vector which are indepently, % and then use eigen vectors to transform into multivariate gaussin random % vector. % % This function uses the randn() in Matlab , so the user can change or reset the % seed of randn(). % % Output % X = a matrix and each column is for one random variable. % % Input % mu = means of random variables, column vector % cx = covariance matrix % N = number of samples % % example: X = randv([0;0;0], [1 0 0; 0 1 0.5; 0 0.5 1], 1500) ; % Copyright : Speech communciation lab, University of Maryland % % Written by Xinhui Zhou - April 15, 2008 % if nargin < 3, eval('help randnv'); return; end; [v, lambda] = eig(cx) ; % randn('state',sum(100*clock)) ; % change the seed of the random generator y = randn(N*length(mu), 1) ; y = reshape(y, N, length(mu)) ; for k = 1:length(mu) y(:,k) = sqrt(lambda(k,k))*y(:,k) ; end X = (y*v') + repmat(mu', N,1) ; return ;