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:
How to avoid nested for loops

Subject: How to avoid nested for loops

From: Volkan

Date: 2 Jul, 2010 01:17:04

Message: 1 of 10

Hi,

I am trying to avoid for loops but I couldn't find an easy solution for the problem below:

p1hf = [1,2;3,4];
nb = 2; % It might go up to 100 or so need a smarter solution
p2hf = zeros(nb, nb, nb, nb);
for a=1:nb
   for b=1:nb
      for c=1:nb
         for d=1:nb
            p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
         end
      end
   end
end

p2hf is 4-D. Is there an easy way to avoid these loops?

Your help will be greatly appreciated.

Subject: How to avoid nested for loops

From: Roger Stafford

Date: 2 Jul, 2010 04:19:05

Message: 2 of 10

"Volkan " <volkaned@hotmail.com> wrote in message <i0jemg$hka$1@fred.mathworks.com>...
> Hi,
>
> I am trying to avoid for loops but I couldn't find an easy solution for the problem below:
>
> p1hf = [1,2;3,4];
> nb = 2; % It might go up to 100 or so need a smarter solution
> p2hf = zeros(nb, nb, nb, nb);
> for a=1:nb
> for b=1:nb
> for c=1:nb
> for d=1:nb
> p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
> end
> end
> end
> end
>
> p2hf is 4-D. Is there an easy way to avoid these loops?
>
> Your help will be greatly appreciated.
- - - - - - - - -
  I think this does the same thing, but I'm not sure any time is saved.

 p2hf = reshape(reshape(p1hf,[],1)*reshape(p1hf,1,[]),nb,nb,nb,nb);
 p2hf = 2*p2hf-permute(p2hf,[1,3,2,4]);

Roger Stafford

Subject: How to avoid nested for loops

From: Zeeshan

Date: 2 Jul, 2010 04:58:05

Message: 3 of 10

Hi Roger

I want to plot a 2D mesh of spherical pore bodies connected with throats. The 2D mesh could be a diamond lattice. The spherical pore bodies is of volume ((4 π/3 )r3 ) = l3 and the throat bodies are cylindrical which are of length l and have randomly chosen cross sectional area between 0 and l2. Also the volume of both pore and throat could be adjusted as desired.

Can you help me in this ?

Subject: How to avoid nested for loops

From: Walter Roberson

Date: 2 Jul, 2010 05:21:08

Message: 4 of 10

"Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...
> Hi Roger
>
> I want to plot a 2D mesh of spherical pore bodies connected with throats.

Please do not add unrelated questions to an existing thread.

Subject: How to avoid nested for loops

From: Roger Stafford

Date: 2 Jul, 2010 05:31:04

Message: 5 of 10

"Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...
> Hi Roger
>
> I want to plot a 2D mesh of spherical pore bodies connected with throats. The 2D mesh could be a diamond lattice. The spherical pore bodies is of volume ((4 π/3 )r3 ) = l3 and the throat bodies are cylindrical which are of length l and have randomly chosen cross sectional area between 0 and l2. Also the volume of both pore and throat could be adjusted as desired.
>
> Can you help me in this ?
- - - - - - - - - -
  That is not ethical, Zeeshan. You have tacked your own question onto another person's entirely different thread. That is not the way to get questions answered. Also you have entered exactly the same question with precisely the same wording (except for the "Hi Roger") in at least four other threads today and yesterday. This is definitely not the way to cause other people to be willing to help you. Duplicate threads are very much frowned upon in this newsgroup. I certainly don't intend to help you.

Roger Stafford

Subject: How to avoid nested for loops

From: Zeeshan

Date: 2 Jul, 2010 06:02:21

Message: 6 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0jtio$agm$1@fred.mathworks.com>...
> "Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...
> > Hi Roger
> >
> > I want to plot a 2D mesh of spherical pore bodies connected with throats. The 2D mesh could be a diamond lattice. The spherical pore bodies is of volume ((4 π/3 )r3 ) = l3 and the throat bodies are cylindrical which are of length l and have randomly chosen cross sectional area between 0 and l2. Also the volume of both pore and throat could be adjusted as desired.
> >
> > Can you help me in this ?
> - - - - - - - - - -
> That is not ethical, Zeeshan. You have tacked your own question onto another person's entirely different thread. That is not the way to get questions answered. Also you have entered exactly the same question with precisely the same wording (except for the "Hi Roger") in at least four other threads today and yesterday. This is definitely not the way to cause other people to be willing to help you. Duplicate threads are very much frowned upon in this newsgroup. I certainly don't intend to help you.
--------
Sorry if it bothers you
Zeeshan

Subject: How to avoid nested for loops

From: Volkan

Date: 2 Jul, 2010 07:22:07

Message: 7 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0jpbo$97t$1@fred.mathworks.com>...
> "Volkan " <volkaned@hotmail.com> wrote in message <i0jemg$hka$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to avoid for loops but I couldn't find an easy solution for the problem below:
> >
> > p1hf = [1,2;3,4];
> > nb = 2; % It might go up to 100 or so need a smarter solution
> > p2hf = zeros(nb, nb, nb, nb);
> > for a=1:nb
> > for b=1:nb
> > for c=1:nb
> > for d=1:nb
> > p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
> > end
> > end
> > end
> > end
> >
> > p2hf is 4-D. Is there an easy way to avoid these loops?
> >
> > Your help will be greatly appreciated.
> - - - - - - - - -
> I think this does the same thing, but I'm not sure any time is saved.
>
> p2hf = reshape(reshape(p1hf,[],1)*reshape(p1hf,1,[]),nb,nb,nb,nb);
> p2hf = 2*p2hf-permute(p2hf,[1,3,2,4]);
>
> Roger Stafford

Thank you so much Roger. I think you are right about cpu usage. It doesn't really save time when nb > 50.

I tried to run it with nb = 2 ( your algorithm was slightly faster), nb =5 ( mine was slightly faster), nb =75 (mine was much faster), and nb =100 (out of memory).

Your solution is brilliant though. I gained a new perspective to consider when dealing with these kinds of problems. Thank you so much again.

Subject: How to avoid nested for loops

From: Jan Simon

Date: 2 Jul, 2010 07:52:05

Message: 8 of 10

Dear Volkan,

> p1hf = [1,2;3,4];
> nb = 2; % It might go up to 100 or so need a smarter solution
> p2hf = zeros(nb, nb, nb, nb);
> for a=1:nb
> for b=1:nb
> for c=1:nb
> for d=1:nb
> p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
> end
> end
> end
> end

Do you just want to omit the loops or do you need more speed?
In the later case you could try this:

p1hf = [1,2; 3,4];
nb = 2; % It might go up to 100 or so need a smarter solution
p2hf = zeros(nb, nb, nb, nb);
for a = 1:nb
   for b = 1:nb
      t1 = 2*p1hf(a,b);
      for c = 1:nb
         p2hf(a,b,c, 1:nb) = t1*p1hf(c, 1:nb) - p1hf(a,c)*p1hf(b, 1:nb);
      end
   end
end

If the dimensions are matching, the "1:nb" index can be omitted:
         p2hf(a,b,c, :) = t1*p1hf(c, :) - p1hf(a,c)*p1hf(b, :);

You can replace the loop over c also:
  ...
for a = 1:nb
   for b = 1:nb
      t1 = 2*p1hf(a,b);
      p2hf(a,b, :, :) = t1*p1hf - transpose(p1hf(a, :)) * p1hf(b, :);
   end
end

(Please test this - I do not have access to Matlab on this computer).

Good luck, Jan

Subject: How to avoid nested for loops

From: Matt Fig

Date: 2 Jul, 2010 12:18:04

Message: 9 of 10

Try this:

p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) -...
         bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));

Subject: How to avoid nested for loops

From: Volkan

Date: 8 Jul, 2010 00:36:03

Message: 10 of 10

"Matt Fig" <spamanon@yahoo.com> wrote in message <i0klds$dl1$1@fred.mathworks.com>...
> Try this:
>
> p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) -...
> bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));

Dear Matt,

It seems that bsxfun is the fastest among the suggested routines taking care of the problem in hand. It is slightly faster than for loops, while keeping the code elegant and compact.

Thanks you all for your help.

Tags for this Thread

No tags are associated with 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