Path: news.mathworks.com!not-for-mail
From: "Per Sundqvist" <sunkan@fy.chalmers.se>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorization for Quadratic Polynomial Regression
Date: Sat, 5 Jul 2008 19:34:02 +0000 (UTC)
Organization: Chalmers Tekniska H&#246;gskola
Lines: 36
Message-ID: <g4oiba$klp$1@fred.mathworks.com>
References: <cddc4bd3-1f78-4fad-90d2-a7cde7ec5189@e39g2000hsf.googlegroups.com> <g4noi9$ja5$1@fred.mathworks.com> <g4npnp$rsb$1@fred.mathworks.com>
Reply-To: "Per Sundqvist" <sunkan@fy.chalmers.se>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1215286442 21177 172.30.248.37 (5 Jul 2008 19:34:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 5 Jul 2008 19:34:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 266682
Xref: news.mathworks.com comp.soft-sys.matlab:477681


"Bruno Luong" <b.luong@fogale.fr> wrote in message
<g4npnp$rsb$1@fred.mathworks.com>...
> Slighly more compact
> 
> X0 = [ 1 2 3; 4 5 6]
> [m n0]=size(X0)
> 
> [I J]=meshgrid(1:n0,1:n0);
> 
> X0_IJ = reshape(X0(:,I).*X0(:,J), m, n0*n0);
> 
> % There might be a better way than using 'find'
> itri=[find(J>I); sub2ind(size(I),1:n0,1:n0)'];
> I=I(itri); J=J(itri);
> 
> X = [X0 X0_IJ(:,sub2ind([n0 n0],I,J))]
> 
> % Bruno

Ok, maby this is a little bit shorter
%--- Prepare some data ---
n=4;  % dimensions
Nd=7; % # of data
X=[ones(Nd,1) rand(Nd,n)];
f=rand(Nd,1);

%solve multi-dim second order least square fit
[I,J]=meshgrid(1:n+1);
X=X(:,I(find(I>=J))).*X(:,J(find(I>=J)));
W=X\f;

% present solution
% term: x_i*x_j,Wij (x_0=1).
[int2str(I(find(I>=J))-1) ... 
int2str(J(find(I>=J))-1) num2str(W,4)]