Makes a vector, matrix or structure into a term in a linear model. Usage: t = term( x [, str] ); Internally a term consists of a cell array of strings for the names of the variables in the term, and a matrix with one column for each name, which can be accessed by char and double (see below). If x is a matrix of numbers, t has one variable for each column of x. If x is a cell array of strings or a matrix whose rows are strings, t is a set of indicator variables for the unique strings in x. If x is a structure, t has one variable for eaxh field of x. If x is a scalar, t is the constant term. It is expanded to the length of the other term in a binary operator. If x is a term, t=x. With no arguments, t is the empty term. str is a cell array of strings for the names of the variables. If absent, the names for the four cases above are either 'x' (followed by 1,2,... if x is a matrix), the unique strings in x, the fields of x, num2str(x) if x is a scalar, or '?' if x is an algebraic expression. Term t can be subscripted and returns a numeric vector or matrix, e.g. t.f = variable with name 'f'. t(1,3:5) = matrix whose columns are variables 1,3,4,5. The following operators are overloaded for terms t1 and t2: t1 + t2 = {variables in t1} union {variables in t2}. t1 - t2 = {variables in t1} intersection complement {variables in t2}. t1 * t2 = sum of the element-wise product of each variable in t1 with each variable in t2, and corresponds to the interaction between t1 and t2. t ^ k = product of t with itself, k times. Algebra: commutative, associative and distributive rules apply to terms: a + b = b + a a * b = b * a (a + b) + c = a + (b + c) (a * b) * c = a * (b * c) (a + b) * c = a * c + b * c a + 0 = a a * 1 = a a * 0 = 0 However note that a + b - c =/= a + (b - c) =/= a - c + b The following functions are overloaded for term t: char(t) = cell array of the names of the variables. double(t) = matrix whose columns are the variables in t, i.e. the design matrix of the linear model. size(t [, dim]) = size(double(t) [, dim]). isempty(t) = 1 if t is empty and 0 if not.
0001 function t = term( x, str ); 0002 0003 %Makes a vector, matrix or structure into a term in a linear model. 0004 % 0005 % Usage: t = term( x [, str] ); 0006 % 0007 % Internally a term consists of a cell array of strings for the names of 0008 % the variables in the term, and a matrix with one column for each name, 0009 % which can be accessed by char and double (see below). 0010 % 0011 % If x is a matrix of numbers, t has one variable for each column of x. 0012 % If x is a cell array of strings or a matrix whose rows are strings, t 0013 % is a set of indicator variables for the unique strings in x. 0014 % If x is a structure, t has one variable for eaxh field of x. 0015 % If x is a scalar, t is the constant term. It is expanded to the length 0016 % of the other term in a binary operator. 0017 % If x is a term, t=x. With no arguments, t is the empty term. 0018 % 0019 % str is a cell array of strings for the names of the variables. If absent, 0020 % the names for the four cases above are either 'x' (followed by 1,2,... if 0021 % x is a matrix), the unique strings in x, the fields of x, num2str(x) if x 0022 % is a scalar, or '?' if x is an algebraic expression. 0023 % 0024 % Term t can be subscripted and returns a numeric vector or matrix, e.g. 0025 % t.f = variable with name 'f'. 0026 % t(1,3:5) = matrix whose columns are variables 1,3,4,5. 0027 % 0028 % The following operators are overloaded for terms t1 and t2: 0029 % t1 + t2 = {variables in t1} union {variables in t2}. 0030 % t1 - t2 = {variables in t1} intersection complement {variables in t2}. 0031 % t1 * t2 = sum of the element-wise product of each variable in t1 with 0032 % each variable in t2, and corresponds to the interaction 0033 % between t1 and t2. 0034 % t ^ k = product of t with itself, k times. 0035 % 0036 % Algebra: commutative, associative and distributive rules apply to terms: 0037 % a + b = b + a 0038 % a * b = b * a 0039 % (a + b) + c = a + (b + c) 0040 % (a * b) * c = a * (b * c) 0041 % (a + b) * c = a * c + b * c 0042 % a + 0 = a 0043 % a * 1 = a 0044 % a * 0 = 0 0045 % However note that 0046 % a + b - c =/= a + (b - c) =/= a - c + b 0047 % 0048 % The following functions are overloaded for term t: 0049 % char(t) = cell array of the names of the variables. 0050 % double(t) = matrix whose columns are the variables in t, i.e. 0051 % the design matrix of the linear model. 0052 % size(t [, dim]) = size(double(t) [, dim]). 0053 % isempty(t) = 1 if t is empty and 0 if not. 0054 0055 if nargin == 0 0056 t.names=[]; 0057 t.matrix=[]; 0058 t = class(t,'term'); 0059 elseif isa(x,'term') 0060 t = x; 0061 elseif iscellstr(x) 0062 u=unique(x); 0063 k=length(u); 0064 t.names=cell(1,k); 0065 t.matrix=zeros(length(x),k); 0066 for i=1:k 0067 % t.names{i}=[inputname(1) '.' u{i}]; 0068 t.names(i)=u(i); 0069 t.matrix(:,i)=ismember(x,u(i)); 0070 end 0071 t = class(t,'term'); 0072 elseif ischar(x) && ndims(x)==2 0073 t.names=unique(x,'rows'); 0074 t.matrix=zeros(length(x),length(t.names)); 0075 for i=1:length(t.names) 0076 t.matrix(:,i)=ismember(x,t.names(i,:),'rows'); 0077 end 0078 t = class(t,'term'); 0079 elseif isnumeric(x) && numel(x)>1 0080 if nargin<2 0081 str={inputname(1)}; 0082 end 0083 if isempty(str) 0084 str={'?'}; 0085 end 0086 if ischar(str) 0087 str={str}; 0088 end 0089 if length(str)==size(x,2) 0090 t.names=str; 0091 else 0092 for k=1:size(x,2) 0093 t.names{k}=[char(str) num2str(k)]; 0094 end 0095 end 0096 t.matrix=double(x); 0097 t = class(t,'term'); 0098 elseif isnumeric(x) && numel(x)==1 0099 t.names={num2str(x)}; 0100 t.matrix=double(x); 0101 t = class(t,'term'); 0102 elseif isstruct(x) 0103 t.names=fieldnames(x)'; 0104 t.matrix=[]; 0105 for i=1:length(t.names) 0106 t.matrix=[t.matrix double(getfield(x,t.names{i}))]; 0107 end 0108 t = class(t,'term'); 0109 end