The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: vectorization loops
Replies: 3   Last Post: Feb 5, 2013 2:35 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 9,850
Registered: 6/7/07
Re: vectorization loops
Posted: Feb 5, 2013 1:22 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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


>> 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 =
>> s=x'.*x; s=sum(s(:))
s =
>> s=sum(sum(x'.*x))
s =
>> s=sum(dot(x',x))
s =

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


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.