Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slgaussrnd
Home > sltoolbox > stat > slgaussrnd.m

slgaussrnd

PURPOSE ^

SLGAUSSRND Generates random samples from Gaussian models

SYNOPSIS ^

function X = slgaussrnd(GS, nums)

DESCRIPTION ^

SLGAUSSRND Generates random samples from Gaussian models

 $ Syntax $
   - X = slgaussrnd(GS, nums)

 $ Arguments $
   - GS:       The Gaussian model struct
   - nums:     the number of samples from the models

 $ Description $
   - X = slgaussrnd(GS, nums) randomly draws samples from Gaussian models.
     Suppose there are k constituent models. Then nums can be of
     the following forms:
       - length-k vector, representing the number of samples of the
         models respectively.

 $ History $
   - Created by Dahua Lin, on Aug 24, 2006
   - Modified by Dahua Lin, on Sep 10, 2006
       - Replace sladd by sladdvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
  • slsymeig SLSYMEIG Compute the eigenvalues and eigenvectors for symmetric matrix
  • slgausstype SLGAUSSTYPE Judges the type of a Gaussian model struct
  • slpartition SLPARTITION Partition a range into blocks in a specified manner
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function X = slgaussrnd(GS, nums)
0002 %SLGAUSSRND Generates random samples from Gaussian models
0003 %
0004 % $ Syntax $
0005 %   - X = slgaussrnd(GS, nums)
0006 %
0007 % $ Arguments $
0008 %   - GS:       The Gaussian model struct
0009 %   - nums:     the number of samples from the models
0010 %
0011 % $ Description $
0012 %   - X = slgaussrnd(GS, nums) randomly draws samples from Gaussian models.
0013 %     Suppose there are k constituent models. Then nums can be of
0014 %     the following forms:
0015 %       - length-k vector, representing the number of samples of the
0016 %         models respectively.
0017 %
0018 % $ History $
0019 %   - Created by Dahua Lin, on Aug 24, 2006
0020 %   - Modified by Dahua Lin, on Sep 10, 2006
0021 %       - Replace sladd by sladdvec to increase efficiency
0022 %
0023 
0024 %% parse and verify input arguments
0025 
0026 tyinfo = slgausstype(GS);
0027 d = GS.dim;
0028 k = GS.nmodels;
0029 
0030 if length(nums) ~= k
0031     error('sltoolbox:invalidarg', ...
0032         'The nums should be a length-k vector');
0033 end
0034 
0035 n = sum(nums);
0036 ps = slpartition(n, 'blksizes', nums);
0037 
0038 %% generate samples
0039 
0040 X = zeros(d, n);
0041 
0042 switch tyinfo.varform
0043     case 'univar'
0044         if tyinfo.sharevar
0045             for i = 1 : k
0046                 si = ps.sinds(i); ei = ps.einds(i);
0047                 X(:, si:ei) = gensamples_univar(GS.means(:,i), GS.vars, d, nums(i));
0048             end
0049         else
0050             for i = 1 : k
0051                 si = ps.sinds(i); ei = ps.einds(i);
0052                 X(:, si:ei) = gensamples_univar(GS.means(:,i), GS.vars(i), d, nums(i));
0053             end                
0054         end
0055         
0056     case 'diagvar'
0057         if tyinfo.sharevar
0058             for i = 1 : k
0059                 si = ps.sinds(i); ei = ps.einds(i);
0060                 X(:, si:ei) = gensamples_diagvar(GS.means(:,i), GS.vars, d, nums(i));
0061             end
0062         else
0063             for i = 1 : k
0064                 si = ps.sinds(i); ei = ps.einds(i);
0065                 X(:, si:ei) = gensamples_diagvar(GS.means(:,i), GS.vars(:,i), d, nums(i));
0066             end                
0067         end
0068         
0069     case 'covar'
0070         if tyinfo.sharevar
0071             for i = 1 : k
0072                 si = ps.sinds(i); ei = ps.einds(i);
0073                 X(:, si:ei) = gensamples_covar(GS.means(:,i), GS.covs, d, nums(i));
0074             end
0075         else
0076             for i = 1 : k
0077                 si = ps.sinds(i); ei = ps.einds(i);
0078                 X(:, si:ei) = gensamples_covar(GS.means(:,i), GS.covs(:,:,i), d, nums(i));
0079             end                
0080         end
0081         
0082     otherwise
0083         error('sltoolbox:invalidarg', ...
0084             'Invalid variance form in GS: %s', tyinfo.varform);
0085         
0086 end
0087 
0088 
0089 %% Core generation routines
0090 
0091 function X = gensamples_univar(vmean, var, d, n)
0092 
0093 X = randn(d, n) * sqrt(max(var, 0));
0094 X = sladdvec(X, vmean, 1);
0095 
0096 function X = gensamples_diagvar(vmean, vars, d, n)
0097 
0098 X = randn(d, n);
0099 X = slmulvec(X, sqrt(max(vars, 0)), 1);
0100 X = sladdvec(X, vmean, 1);
0101 
0102 function X = gensamples_covar(vmean, C, d, n)
0103 
0104 X = randn(d, n);
0105 [D, V] = slsymeig(C);
0106 T = V * diag(sqrt(max(D, 0)));
0107 clear D V;
0108 X = T * X;
0109 clear T;
0110 X = sladdvec(X, vmean, 1);
0111 
0112 
0113 
0114 
0115 
0116 
0117 
0118 
0119 
0120 
0121

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com