Thread Subject: Vectorization of Nested Loop

Subject: Vectorization of Nested Loop

From: Priya Narayanan

Date: 27 Aug, 2009 00:48:01

Message: 1 of 6

Hello:

I'm having trouble vectorizing nested loops. Eg. How do I vectorize this simple loop?

 for i =1:10
     for j= 1:10
        for k=1:10

 A(i,j,k)= i *j * k;
        end
     end
 end


Thanks,
-P

Subject: Vectorization of Nested Loop

From: omegayen

Date: 27 Aug, 2009 00:56:04

Message: 2 of 6

"Priya Narayanan" <priya.nambiar@gmail.com> wrote in message <h74l41$eom$1@fred.mathworks.com>...
> Hello:
>
> I'm having trouble vectorizing nested loops. Eg. How do I vectorize this simple loop?
>
> for i =1:10
> for j= 1:10
> for k=1:10
>
> A(i,j,k)= i *j * k;
> end
> end
> end
>
>
> Thanks,
> -P


This may give you a start http://www.mathworks.com/matlabcentral/newsreader/view_thread/257423

Subject: Vectorization of Nested Loop

From: Jan Simon

Date: 27 Aug, 2009 09:52:01

Message: 3 of 6

Dear Priya Narayanan!

> I'm having trouble vectorizing nested loops. Eg. How do I vectorize this simple loop?
> for i =1:10
> for j= 1:10
> for k=1:10
> A(i,j,k)= i *j * k;
> end
> end
> end

You can start with a standard method: insert the innermost FOR loop in the calculation:
  A = zeros(10, 10, 10); % Pre-allocate!!!
  for i = 1:10
      for j = 1:10
         A(i, j, 1:10) = i * j * (1:10);
      end
  end

After the pre-allocation, you can omit the indexing for the last dimension in A:
  A(i, j, :) = ... % Is enough and faster

In the next step, moving the vector from the FOR loop into the calculation needs a further method, because (1:10) * (1:10) does not create a 2D matrix. But the following does:
  A = zeros(10, 10, 10); % Pre-allocate!!!
  for i = 1:10
     A(i, :, :) = i * transpose(1:10) * (1:10);
  end
As you see, the time-consuming "transpose(1:10) * (1:10)" does not change its value in the loop, so it should be move outside:
  A = zeros(10, 10, 10); % Pre-allocate!!!
  X = transpose(1:10) * (1:10);
  for i = 1:10
     A(i, :, :) = i * X;
  end
From here on I do not know a trivial standard mechanism for further vectorization, but of course it is possible to avoid the last loop also.

If the array is larger, it is usually more effective to process the first dimension at once, because the processed data match into the small processor cache. So it's worth to try something with "A(:, :, k)" instead of the above method.

Have fun when proceeding from this simple start, Jan

Subject: Vectorization of Nested Loop

From: Bruno Luong

Date: 27 Aug, 2009 10:05:20

Message: 4 of 6

One of many solutions:

i=1:10;
j=1:10;
k=1:10;

A = zeros(length(i),length(j),length(k));
A(:) = reshape(i.'*j,[],1)*k;

% Bruno

Subject: Vectorization of Nested Loop

From: Loren Shure

Date: 27 Aug, 2009 12:08:57

Message: 5 of 6

In article <h75lp0$e9v$1@fred.mathworks.com>,
b.luong@fogale.findmycountry says...
> One of many solutions:
>
> i=1:10;
> j=1:10;
> k=1:10;
>
> A = zeros(length(i),length(j),length(k));
> A(:) = reshape(i.'*j,[],1)*k;
>
> % Bruno
>

consider also ndgrid (memory intensive) and bsxfun (will take multiple
statements).

--
Loren
http://blogs.mathworks.com/loren

Subject: Vectorization of Nested Loop

From: Priya Narayanan

Date: 27 Aug, 2009 23:53:02

Message: 6 of 6

Thank you. As a followup question-is it possible to vectorize this loop
for d = 1:8
    for f_b = 1: 2
         for t = 1:5
              spvec = [KinDat.sub(s).case(c).dir(d).fwd_bwd(f_b).trial(t).sp];
          end;
      end;
end

"Priya Narayanan" <priya.nambiar@gmail.com> wrote in message <h74l41$eom$1@fred.mathworks.com>...
> Hello:
>
> I'm having trouble vectorizing nested loops. Eg. How do I vectorize this simple loop?
>
> for i =1:10
> for j= 1:10
> for k=1:10
>
> A(i,j,k)= i *j * k;
> end
> end
> end
>
>
> Thanks,
> -P

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
vectorization Priya Narayanan 26 Aug, 2009 20:49:04
rssFeed for this Thread

Contact us at files@mathworks.com