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:
vectorization loops

Subject: vectorization loops

From: Christian

Date: 5 Feb, 2013 17:17:14

Message: 1 of 5

Hello,
does there exist something like an approach for vectorization loops (2, 3 or 4 nested loops)?
I mean using ndgrid, reshape, repmat, sub2ind, ... or whatever.
I already have read a lot here in the newsgroup and the MATLAB help. Bruno show some very good examples here :) but mostly without some explanation. So I still encounter difficulties.
Example for discussion:

n=3;
a=rand(n,n);
for i=1:n
    for j=1:n
        b = b + a(i,j)*a(j,i); % command
    end
end
b

Okay, the innermost loop is absolutely no problem, but what's with the outer one.
Christian

Subject: vectorization loops

From: Steven_Lord

Date: 5 Feb, 2013 18:16:09

Message: 2 of 5



"Christian " <muell.halde@iwu.fraunhofer.de> wrote in message
news:kereqq$rnb$1@newscl01ah.mathworks.com...
> Hello,
> does there exist something like an approach for vectorization loops (2, 3
> or 4 nested loops)?

Be more specific, please. There are many different techniques you can use to
write MATLAB code; loops and vectorization are two such techniques.

> I mean using ndgrid, reshape, repmat, sub2ind, ... or whatever.
> I already have read a lot here in the newsgroup and the MATLAB help. Bruno
> show some very good examples here :) but mostly without some explanation.
> So I still encounter difficulties.
> Example for discussion:
>
> n=3;
> a=rand(n,n);
> for i=1:n
> for j=1:n
> b = b + a(i,j)*a(j,i); % command
> end
> end
> b

So you want to take each element of a, multiply it by its reflection in the
main diagonal, and add up all the resulting values?

eachTerm = a.*a.';
b = sum(eachTerm(:));

The first line performs all the operations a(i, j)*a(j, i) at once using
elementwise multiplication and the nonconjugate transpose (in case a
contains some complex values in your real problem, of which this is a
simplified example.) The second line performs the summation.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: vectorization loops

From: dpb

Date: 5 Feb, 2013 18:22:20

Message: 3 of 5

On 2/5/2013 11:17 AM, Christian wrote:
> Hello,
> does there exist something like an approach for vectorization loops (2,
> 3 or 4 nested loops)?
...

Completely general, no, most have to be analyzed based on the
characteristics of what is in the loop(s) and the loop structure itself...

> Example for discussion:
>
> n=3;
> a=rand(n,n);
> for i=1:n
> for j=1:n
> b = b + a(i,j)*a(j,i); % command
> end
> end
> b
>
> Okay, the innermost loop is absolutely no problem, but what's with the
> outer one.

Consider what the above operations do...for this particular case,

s=x'.*x; s=sum(s(:);

which can be written in several other ways...

s=sum(sum(x'.*x));
s=sum(dot(x'.*x));

 >> x=rand(3);
 >> s=0;for i=1:3,for j=1:3,s=s+x(i,j)*x(j,i);end,end,s
s =
     3.1787
 >> s=x'.*x; s=sum(s(:))
s =
     3.1787
 >> s=sum(sum(x'.*x))
s =
     3.1787
 >> s=sum(dot(x',x))
s =
     3.1787
 >>

So, the key is in recognizing the algebraic expression and often on how
it relates to linear algebra, etc., ...

--

Subject: vectorization loops

From: Bruno Luong

Date: 5 Feb, 2013 19:35:11

Message: 4 of 5

Other ways:

s = trace(x^2)

s = reshape(x,1,[])*reshape(x',[],1)

% Bruno

Subject: vectorization loops

From: Christian

Date: 5 Feb, 2013 21:14:14

Message: 5 of 5

Thank you for your fast replies and detailed explanations! It helps me much!
I will try some more examples.

Regarding the example, even just s = trace(a^2) seems to be a correct substitution for the loops.

Chris

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