Home > atmlab > randomize > randmvar_normal.m

randmvar_normal

PURPOSE ^

RANDMVAR_NORMAL Random multivariate normal distributed data

SYNOPSIS ^

function X = randmvar_normal(x,si,C,n)

DESCRIPTION ^

 RANDMVAR_NORMAL  Random multivariate normal distributed data

    Creates a set of random vectors fulfilling multivariate normal
    statistics. The statistics to fulfill is defined by giving the
    mean vector, standard deviations and the correlation matrix.

    See also *randmvar_normal2*.

 FORMAT   X = randmvar_normal( x, si, C, n )

 OUT   X    Random data [length(x) x n].
 IN    x    Mean vector. Vector with length matching *C* or a scalar.
            In the scalar case, this value is applied for all positions.
       si   Standard deviations. Vector with length matching *C* or a scalar.
            In the scalar case, this value is applied for all positions.
       C    Correlation matrix.
       n    Number of realisations to generate.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

randmvar_normal.m

SOURCE CODE ^

0001 % RANDMVAR_NORMAL  Random multivariate normal distributed data
0002 %
0003 %    Creates a set of random vectors fulfilling multivariate normal
0004 %    statistics. The statistics to fulfill is defined by giving the
0005 %    mean vector, standard deviations and the correlation matrix.
0006 %
0007 %    See also *randmvar_normal2*.
0008 %
0009 % FORMAT   X = randmvar_normal( x, si, C, n )
0010 %
0011 % OUT   X    Random data [length(x) x n].
0012 % IN    x    Mean vector. Vector with length matching *C* or a scalar.
0013 %            In the scalar case, this value is applied for all positions.
0014 %       si   Standard deviations. Vector with length matching *C* or a scalar.
0015 %            In the scalar case, this value is applied for all positions.
0016 %       C    Correlation matrix.
0017 %       n    Number of realisations to generate.
0018 
0019 % 2005-05-20   Created by Patrick Eriksson.
0020 
0021 
0022 function X = randmvar_normal(x,si,C,n)
0023 
0024 
0025 %= Check input
0026 %
0027 rqre_nargin( 4, nargin );
0028 %
0029 if dimens(C) > 2  |  size(C,1) ~= size(C,2) 
0030   error('Input argument *C* must be a square matrix.');
0031 end
0032 %
0033 l = size(C,1);
0034 %
0035 if ~( length(x) == 1  |  length(x) == l )
0036   error( ...
0037   'Input argument *x* must be a scalar or a vector with length matching *C*.');
0038 end
0039 %
0040 if ~( length(si) == 1  |  length(si) == l )
0041   error( ...
0042  'Input argument *si* must be a scalar or a vector with length matching *C*.');
0043 end
0044 %
0045 if ~isscalar(n)  |  ~iswhole(n)  |  n<1
0046   error('Input argument *n* must be a positive integer.');
0047 end
0048 
0049 
0050 if length(si) > 1
0051   si = vec2col( si );
0052 end
0053 
0054 S = (si*si') .* C; 
0055 
0056 if length(x) == 1
0057   x = repmat( x, l, 1 );;
0058 end
0059 
0060 
0061 X = repmat(vec2col(x),1,n) + chol(S)'*randn(l,n);

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005