-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcovdraw.m
36 lines (29 loc) · 881 Bytes
/
covdraw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function c = covdraw(C, n)
% covdraw
%
% Draw n random samples from a zero-mean Gaussian distribution with
% covariance C.
%
% Inputs:
%
% C Covariance matrix (nC-by-nC)
% n Number of samples to draw (default is 1)
%
% Outputs:
%
% c Draws from C (nC-by-n)
%
% Copyright 2016 An Uncommon Lab
% Defaults
if nargin < 2, n = 1; end;
% Use singular value decomposition instead of Cholesky, because
% Cholesky chokes when the matrix is positive semi-definite, and we
% want to be able to handle positive semi-definite matrices.
[u, s] = svd(C);
s(s < 0) = 0; % Nothing should be negative, but just in case...
s = sqrt(s);
% At this point, C == (u * s) * (u * s).', so (u * s) is the Cholesky
% factor (if C was positive definite).
% Construct the draws.
c = u * s * randn(size(C, 1), n);
end % covdraw