Thread Subject: Vectorization

Subject: Vectorization

From: laxxy

Date: 4 Jul, 2009 20:24:27

Message: 1 of 7

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... :(

Subject: Vectorization

From: Yi Cao

Date: 4 Jul, 2009 21:27:00

Message: 2 of 7

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

Subject: Vectorization

From: laxxy

Date: 5 Jul, 2009 11:47:11

Message: 3 of 7

> > 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).
>
> W=Q*V(repmat((1:K)',1,N)+(P-1)*K);

Thanks!
But this is not quite the same though: in your solution each row of
the V(...) is permuted differently; but the i'th iteration of the loop
computes the same permutation on all rows of V which is specified
given by P(i,:)...

Subject: Vectorization

From: Rune Allnor

Date: 5 Jul, 2009 12:05:03

Message: 4 of 7

On 4 Jul, 22:24, laxxy <laxyf...@gmail.com> wrote:
> 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... :(

What about

W(1:K,:) = Q(1:K,:)*V(:,P(1:K,:));

Rune

Subject: Vectorization

From: Bruno Luong

Date: 5 Jul, 2009 12:36:01

Message: 5 of 7

W = Q*V;
W = W(bsxfun(@plus,(1:K)',(P-1)*K))

% Bruno

Subject: Vectorization

From: Yi Cao

Date: 5 Jul, 2009 19:07:00

Message: 6 of 7

laxxy <laxyfoxx@gmail.com> wrote in message <491153fb-0184-4438-997d-cacf25797dea@i6g2000yqj.googlegroups.com>...
> > > 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).
> >
> > W=Q*V(repmat((1:K)',1,N)+(P-1)*K);
>
> Thanks!
> But this is not quite the same though: in your solution each row of
> the V(...) is permuted differently; but the i'th iteration of the loop
> computes the same permutation on all rows of V which is specified
> given by P(i,:)...

Bruno is right. Instead of permutating before multiplying, it should be after the production. Try the following code:

% prepare some data
K=10;
N=20;
Q=randn(K);
V=randn(K,N);
[Z,P]=sort(V,2); % produce some permutation

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

%original loop
W=zeros(K,N);
for i=1:K
W(i,:) = Q(i,:)*V(:,P(i,:));
end;

%compare difference
norm(W-Z)

% should be very small

HTH
Yi

Subject: Vectorization

From: laxxy

Date: 6 Jul, 2009 02:46:56

Message: 7 of 7

Very cute -- thanks a lot Yi & Bruno!!

> % vectorized
> Z=Q*V;
> Z=Z(repmat((1:K)',1,N)+(P-1)*K);
>
> %original loop
> W=zeros(K,N);
> for i=1:K
> W(i,:) = Q(i,:)*V(:,P(i,:));
> end;

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
repmat Sprinceana 5 Jul, 2009 04:19:00
reference Sprinceana 5 Jul, 2009 04:19:00
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com