Home > SurfStat > gl.m

gl

PURPOSE ^

Generates levels for a factor.

SYNOPSIS ^

function f = gl( varargin );

DESCRIPTION ^

Generates levels for a factor.

 Usage: f = gl( levels, m, n ); 

 levels = a single scalar, p, or
        = a cell array of p strings {'F1', 'F2', ..., 'Fp'} for the level
        names, or
        = a string 'F', followed by a single scalar p, so that the call is 
        gl( 'F', p, g, n ); the level names are 'F1', 'F2', ..., 'Fp'.
 m      = number or repetitions of each level.
 n      = length of the factor (number of observations).

 f =  {'F1', 'F1', ..., 'F1' (repeated m times), 
       'F2', 'F2', ..., 'F2' (repeated m times),  
       ...
       'Fp', 'Fp', ..., 'Fp' (repeated m times), 
       then all this is repeated until n observations are reached}.
 e.g. 
 gl( 3, 2, 13 ) gives
 {'F1','F1','F2','F2','F3','F3','F1','F1','F2','F2','F3','F3','F1'}

 gl( 's', 3, 2, 13 ) gives
 {'s1','s1','s2','s2','s3','s3','s1','s1','s2','s2','s3','s3','s1'}

 gl( {'a','bb','c'}, 2, 13 ) gives
 { 'a', 'a','bb','bb', 'c', 'c', 'a', 'a','bb','bb', 'c', 'c', 'a'}

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function f = gl( varargin );
0002 
0003 %Generates levels for a factor.
0004 %
0005 % Usage: f = gl( levels, m, n );
0006 %
0007 % levels = a single scalar, p, or
0008 %        = a cell array of p strings {'F1', 'F2', ..., 'Fp'} for the level
0009 %        names, or
0010 %        = a string 'F', followed by a single scalar p, so that the call is
0011 %        gl( 'F', p, g, n ); the level names are 'F1', 'F2', ..., 'Fp'.
0012 % m      = number or repetitions of each level.
0013 % n      = length of the factor (number of observations).
0014 %
0015 % f =  {'F1', 'F1', ..., 'F1' (repeated m times),
0016 %       'F2', 'F2', ..., 'F2' (repeated m times),
0017 %       ...
0018 %       'Fp', 'Fp', ..., 'Fp' (repeated m times),
0019 %       then all this is repeated until n observations are reached}.
0020 % e.g.
0021 % gl( 3, 2, 13 ) gives
0022 % {'F1','F1','F2','F2','F3','F3','F1','F1','F2','F2','F3','F3','F1'}
0023 %
0024 % gl( 's', 3, 2, 13 ) gives
0025 % {'s1','s1','s2','s2','s3','s3','s1','s1','s2','s2','s3','s3','s1'}
0026 %
0027 % gl( {'a','bb','c'}, 2, 13 ) gives
0028 % { 'a', 'a','bb','bb', 'c', 'c', 'a', 'a','bb','bb', 'c', 'c', 'a'}
0029 
0030 if isnumeric(varargin{1})
0031     p=varargin{1};
0032     m=varargin{2};
0033     n=varargin{3};
0034     for i=1:p
0035         levelnames{i}=['F' num2str(i)];
0036     end
0037 end
0038 if iscell(varargin{1})
0039     levelnames=varargin{1};
0040     p=length(levelnames);
0041     m=varargin{2};
0042     n=varargin{3};
0043 end
0044 if isstr(varargin{1})
0045     p=varargin{2};
0046     m=varargin{3};
0047     n=varargin{4};
0048     for i=1:p
0049         levelnames{i}=[varargin{1} num2str(i)];
0050     end
0051 end
0052 
0053 [a,b,c]=ind2sub([m p ceil(n/(m*p))],1:n);
0054 f=levelnames(b)';
0055 
0056 return
0057 end
0058     
0059 
0060

Generated on Fri 26-Sep-2008 14:05:29 by m2html © 2003