function S = normalsamples(m, R, num)
% NORMALSAMPLES Generate samples from normal distribution.
%	S = normalsamples(m, R, num) takes (n x 1) vector m, (n x n) matrix
%	R (positive definite), and scalar parameter num, and returns a (n x
%	num) matrix, the columns of which are random samples from a N[m, R] 
%	distribution.  If num is not provided, one sample is assumed.

error( nargchk(2, 3, nargin) );

if nargin<3, num=1;, end
if size(R,1)~=size(R,2), error('R must be square.'), end
if size(m,2)~=1, error('m must be a column vector.'), end
if size(m,1)~=size(R,1), error('m must have as many rows as R has rows/columns'), end
if (size(num,1)~=1) | (size(num,2)~=1), error('num must be scalar'), end

% Dimension of data samples
N = size(m,1);

% Find the transformation matrix required for the generation.  If R is
% not positive definite this will return an error.
A=chol(R);

% Generate random samples (as column vectors) with identity covariance
% matrix.
S=randn(N,num);

% Transform the data samples
S=A'*S;

% Add in the required mean
S=S+m*ones(1,num);






