Path: news.mathworks.com!not-for-mail
From: "Yi Cao" <y.cao@cranfield.ac.uk>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorization
Date: Sat, 4 Jul 2009 21:27:00 +0000 (UTC)
Organization: Cranfield University
Lines: 23
Message-ID: <h2ohf4$ef0$1@fred.mathworks.com>
References: <8d98157c-ab47-40cb-8e4f-af119371a0db@h8g2000yqm.googlegroups.com>
Reply-To: "Yi Cao" <y.cao@cranfield.ac.uk>
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 1246742820 14816 172.30.248.37 (4 Jul 2009 21:27:00 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 4 Jul 2009 21:27:00 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 69713
Xref: news.mathworks.com comp.soft-sys.matlab:552815


laxxy <laxyfoxx@gmail.com> wrote in message <8d98157c-ab47-40cb-8e4f-af119371a0db@h8g2000yqm.googlegroups.com>...
> Hi all --
> 
> I am wondering if it is possible to eliminate the loop in the
> following piece of code:
> 
> for i=1:K
>   W(i,:) = Q(i,:)*V(:,P(i,:));
> end;
> 
> (that is: each row i of the result is computed by multiplying ith row
> of matrix Q by a permutation of columns of V specified by the i'th row
> of P).
> 
> Here Q is a KxK square matrix; and all other matrices are KxN. P is an
> existing index matrix containing indices into columns of V.
> 
> I've tried a few things but so far have not had too much success... :(

W=Q*V(repmat((1:K)',1,N)+(P-1)*K);

HTH
Yi