Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
looping/vectorising issue...

Subject: looping/vectorising issue...

From: Rich

Date: 12 Feb, 2009 11:01:04

Message: 1 of 5

Anyone have any suggestions on how to vectorise this loop? (by vectorise, i guess i mean converting to matrix multiplication)

I'm at a loss!

dimensions are:
z1(m,n)
z2(m,k)
z3(m,n*k)

for i=1:size(z1,1)
    z3(i,:)=reshape(z1(i,:)'*z2(i,:),1,[]);
end



thanks heaps!

Subject: looping/vectorising issue...

From: Roger Stafford

Date: 12 Feb, 2009 16:57:01

Message: 2 of 5

"Rich " <richardwood@removethishotmail.com> wrote in message <gn0vhg$rn$1@fred.mathworks.com>...
> Anyone have any suggestions on how to vectorise this loop? (by vectorise, i guess i mean converting to matrix multiplication)
>
> I'm at a loss!
>
> dimensions are:
> z1(m,n)
> z2(m,k)
> z3(m,n*k)
>
> for i=1:size(z1,1)
> z3(i,:)=reshape(z1(i,:)'*z2(i,:),1,[]);
> end
>
> thanks heaps!

 [I,J] = ndgrid(1:n,1:k);
 z3 = z1(:,I).*z2(:,J);

Roger Stafford

Subject: looping/vectorising issue...

From: Rich

Date: 13 Feb, 2009 10:06:02

Message: 3 of 5

thanks Roger, you are a legend, and have saved me 75% of my runtime!

Subject: looping/vectorising issue...

From: Rich

Date: 18 Jun, 2009 11:40:20

Message: 4 of 5

So I hit memory problems as soon as I do this for big matrices (thousands by thousands), maybe I should have mentioned that before.
So, does anyone have a less memory intensive version?

I am generally working with sparse matrices, so I think I have to kick my brain into gear enough to work out how to use the find command in this context. But any pointers would be much appreciated!

thanks

Subject: looping/vectorising issue...

From: Rune Allnor

Date: 18 Jun, 2009 13:53:09

Message: 5 of 5

On 18 Jun, 13:40, "Rich " <richardw...@removethishotmail.com> wrote:
> So I hit memory problems as soon as I do this for big matrices (thousands by thousands), maybe I should have mentioned that before.
> So, does anyone have a less memory intensive version?

That's the problem with 'vectorization': You gain some speed at
the expense of memory.

If you're hitting the memory wall, you have two options:

1) Buy a bigger computer.
2) Implement a MEX routine in C or C++.

Rune

Tags for this Thread

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.

Contact us