Line | |
---|
1 | function x = randnorm(n, m, S, V) |
---|
2 | % RANDNORM Sample from multivariate normal. |
---|
3 | % RANDNORM(n,m) returns a matrix of n columns where each column is a sample |
---|
4 | % from a multivariate normal with mean m (a column vector) and unit variance. |
---|
5 | % RANDNORM(n,m,S) specifies the standard deviation, or more generally an |
---|
6 | % upper triangular Cholesky factor of the covariance matrix. |
---|
7 | % This is the most efficient option. |
---|
8 | % RANDNORM(n,m,[],V) specifies the covariance matrix. |
---|
9 | % |
---|
10 | % Example: |
---|
11 | % x = randnorm(5, zeros(3,1), [], eye(3)); |
---|
12 | |
---|
13 | if nargin == 1 |
---|
14 | x = randn(1,n); |
---|
15 | return; |
---|
16 | end |
---|
17 | [d,nm] = size(m); |
---|
18 | x = randn(d, n); |
---|
19 | if nargin > 2 |
---|
20 | if nargin == 4 |
---|
21 | if d == 1 |
---|
22 | S = sqrt(V); |
---|
23 | else |
---|
24 | S = chol(V); |
---|
25 | end |
---|
26 | end |
---|
27 | if d == 1 |
---|
28 | x = S .* x; |
---|
29 | else |
---|
30 | x = S' * x; |
---|
31 | end |
---|
32 | end |
---|
33 | if nm == 1 |
---|
34 | x = x + repmat(m, 1, n); |
---|
35 | else |
---|
36 | x = x + m; |
---|
37 | end |
---|
Note: See
TracBrowser
for help on using the repository browser.