function xx = degexpand(x,deg,addbias) % X = degexpand(x,m) % Takes matrix x in which each column is an example. % Produces matrix X such that x(i,:)=[1 x_1 x_1^2 ... x_1^m x_2 ... x_d^m] % Also normalizes each column of X to the range [-1 1] % X = degexpand(x,m,0) skips adding the ones as the first column in X % (c) Greg Shakhnarovich MIT 2002 if (nargin < 3) addbias=1; end % and also scales down each feature. [m,n] = size(x) ; xx = reshape( repmat(x',[1,1,deg]) .^ ... repmat(reshape(1:deg,[1,1,deg]),[n,m]) ... ,[n,m*deg]) ; % Scale down to numbers in [-1,1] xx = xx / diag(max(abs(xx))) ; if (addbias) xx = [ones(size(xx,1),1) xx]; end