Thread Subject: How can I get rid of this (simple) for-loop?? (!)

Subject: How can I get rid of this (simple) for-loop?? (!)

From: adam

Date: 8 Apr, 2009 04:00:59

Message: 1 of 10

I *know* (and by know I mean I *feel like*) I can do this computation
without using a for-loop, but I just can't figure it out! This is the
bottle neck in my code, so I really want to vectorize it. Here, m is
small and N is larrrrrge.

gs=zeros(m,N);
gs(:,1)=B*A(1,:)' .* A(1,:); % just a vector here...

Here is the loop that I know I do without using a loop somehow:

for j=2:N
     gs(:,j)=gs(:,j-1) + B*A(j,:)' .* A(j,:);
end

So you see we're kind of doing an incremental update as j increases. I
was thinking some sort of cumsum implementation, but like I said it
isn't obvious.

Thanks for any and all advice,

Adam Attarian
NCSU

Subject: How can I get rid of this (simple) for-loop?? (!)

From: Matt Fig

Date: 8 Apr, 2009 04:22:01

Message: 2 of 10

What are the dimensions of A and B? And as optimizing will often depend on variables involved, what are typical values of m,N?

Subject: How can I get rid of this (simple) for-loop?? (!)

From: Bruno Luong

Date: 8 Apr, 2009 07:24:02

Message: 3 of 10

adam <adam.attarian@gmail.com> wrote in message <36c1c0de-e0be-4cb2-bd31-6a1165a53f6b@g37g2000yqn.googlegroups.com>...

> gs=zeros(m,N);
> gs(:,1)=B*A(1,:)' .* A(1,:); % just a vector here...
>
> Here is the loop that I know I do without using a loop somehow:
>
> gs(:,j)=gs(:,j-1) + B*A(j,:)' .* A(j,:);

There is something suspected about the dimensions. They don't match well, or some matrix must be reduced to vector or scalar. I guess the above expression is wrong or not what you intend to compute.

Bruno

Subject: How can I get rid of this (simple) for-loop?? (!)

From: adam

Date: 8 Apr, 2009 15:06:50

Message: 4 of 10

On Apr 8, 3:24=A0am, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> adam <adam.attar...@gmail.com> wrote in message <36c1c0de-e0be-4cb2-bd31-=
6a1165a53...@g37g2000yqn.googlegroups.com>...
> > gs=3Dzeros(m,N);
> > gs(:,1)=3DB*A(1,:)' .* A(1,:); % just a vector here...
>
> > Here is the loop that I know I do without using a loop somehow:
>
> > =A0 =A0 =A0gs(:,j)=3Dgs(:,j-1) + B*A(j,:)' .* A(j,:);
>
> There is something suspected about the dimensions. They don't match well,=
 or some matrix must be reduced to vector or scalar. I guess the above expr=
ession is wrong or not what you intend to compute.
>
> Bruno

The dimensions actually match up.

Lets see:

In this case, A is 489 by 6, so N=3D489 and m=3D6. B is square at m x m =3D
6x6, so B*A(1,:)' .* A(1,:) makes sense.

gs(:,j) is just a column vector, m by 1. gs will end up being m by N
at the end of the for loop.

So the loops and the dimensions all make sense, I just feel that I
should be able to reduce the computation to a mat-vec operation?

Thanks,

Adam

Subject: How can I get rid of this (simple) for-loop?? (!)

From: adam

Date: 8 Apr, 2009 15:12:59

Message: 5 of 10

On Apr 8, 12:22=A0am, "Matt Fig" <spama...@yahoo.com> wrote:
> What are the dimensions of A and B? =A0And as optimizing will often depen=
d on variables involved, what are typical values of m,N?

A is N by m, where m is 6 and N is about 480.
B is square at m by m.

Thanks!

Subject: How can I get rid of this (simple) for-loop?? (!)

From: Bruno Luong

Date: 8 Apr, 2009 15:21:02

Message: 6 of 10

adam <adam.attarian@gmail.com> wrote in message <6d9cbcf3-c05a-43eb-
> A is N by m, where m is 6 and N is about 480.
> B is square at m by m.


N=480; m=6;
A=rand(N,m);
B=rand(m,m);

B*A(1,:)' .* A(1,:)

??? Error using ==> times
Matrix dimensions must agree.

Bruno

Subject: How can I get rid of this (simple) for-loop?? (!)

From: Bruno Luong

Date: 8 Apr, 2009 15:34:01

Message: 7 of 10

adam <adam.attarian@gmail.com> wrote in message <37270691-964b-460c-8bcd-154bbaa77509@r33g2000yqn.googlegroups.com>...

>
> Lets see:
>
> In this case, A is 489 by 6, so N=3D489 and m=3D6. B is square at m x m =3D
> 6x6, so B*A(1,:)' .* A(1,:) makes sense.
>

Let's see B: 6x6
A(1,:): 1x6
A(1,:)': 6x1

 B*A(1,:)' is 6x1, still Fine

 A(1,:) is 1x6

Now my Matlab cannot apply element-wise multiplication operation ".*" between 6x1 and 1x6 matrices.

Or your Matlab must be very special.

Bruno

Subject: How can I get rid of this (simple) for-loop?? (!)

From: adam

Date: 8 Apr, 2009 16:18:09

Message: 8 of 10

On Apr 8, 11:34=A0am, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> adam <adam.attar...@gmail.com> wrote in message <37270691-964b-460c-8bcd-=
154bbaa77...@r33g2000yqn.googlegroups.com>...
>
> > Lets see:
>
> > In this case, A is 489 by 6, so N=3D3D489 and m=3D3D6. B is square at m=
 x m =3D3D
> > 6x6, so B*A(1,:)' .* A(1,:) makes sense.
>
> Let's see B: 6x6
> A(1,:): 1x6
> A(1,:)': 6x1
>
> =A0B*A(1,:)' =A0is 6x1, still Fine
>
> =A0A(1,:) is 1x6
>
> Now my Matlab cannot apply element-wise multiplication operation ".*" =A0=
between 6x1 and 1x6 matrices.
>
> Or your Matlab must be very special.
>
> Bruno

Ok, you're right. There was a typo in my code that I reproduced. It
should be:

B*A(1,:)' .* A(1,:)'.

My bad in copying the code from window to window! This should make
sense.

Adam

Subject: How can I get rid of this (simple) for-loop?? (!)

From: Bruno Luong

Date: 8 Apr, 2009 17:15:04

Message: 9 of 10


> B*A(1,:)' .* A(1,:)'.
>
> My bad in copying the code from window to window! This should make
> sense.

Yes, now it makes sense

m=3;
N=5;

B=rand(m);
A=rand(N,m);

% Engine Bruno
gsBruno=cumsum(A'.*(B*A'),2)

% Engine 2
gs=zeros(m,N);
gs(:,1)=B*A(1,:)' .* A(1,:)'; % just a vector here...

for j=2:N
     gs(:,j)=gs(:,j-1) + B*A(j,:)' .* A(j,:)';
end

% Bruno

Subject: How can I get rid of this (simple) for-loop?? (!)

From: adam

Date: 8 Apr, 2009 17:58:44

Message: 10 of 10

Bruno,

Thank you very much! I knew I could use cumsum() in some manner. Your
code executes in .0007 seconds whereas my loop took .07 seconds to
run.

Thanks again!

Adam Attarian

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com