http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977
MATLAB Central Newsreader  How to avoid nested for loops
Feed for thread: How to avoid nested for loops
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 02 Jul 2010 01:17:04 +0000
How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759271
Volkan
Hi,<br>
<br>
I am trying to avoid for loops but I couldn't find an easy solution for the problem below:<br>
<br>
p1hf = [1,2;3,4];<br>
nb = 2; % It might go up to 100 or so need a smarter solution<br>
p2hf = zeros(nb, nb, nb, nb);<br>
for a=1:nb<br>
for b=1:nb<br>
for c=1:nb<br>
for d=1:nb<br>
p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d)  p1hf(a,c)*p1hf(b,d);<br>
end<br>
end<br>
end<br>
end<br>
<br>
p2hf is 4D. Is there an easy way to avoid these loops?<br>
<br>
Your help will be greatly appreciated.

Fri, 02 Jul 2010 04:19:05 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759289
Roger Stafford
"Volkan " <volkaned@hotmail.com> wrote in message <i0jemg$hka$1@fred.mathworks.com>...<br>
> Hi,<br>
> <br>
> I am trying to avoid for loops but I couldn't find an easy solution for the problem below:<br>
> <br>
> p1hf = [1,2;3,4];<br>
> nb = 2; % It might go up to 100 or so need a smarter solution<br>
> p2hf = zeros(nb, nb, nb, nb);<br>
> for a=1:nb<br>
> for b=1:nb<br>
> for c=1:nb<br>
> for d=1:nb<br>
> p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d)  p1hf(a,c)*p1hf(b,d);<br>
> end<br>
> end<br>
> end<br>
> end<br>
> <br>
> p2hf is 4D. Is there an easy way to avoid these loops?<br>
> <br>
> Your help will be greatly appreciated.<br>
        <br>
I think this does the same thing, but I'm not sure any time is saved.<br>
<br>
p2hf = reshape(reshape(p1hf,[],1)*reshape(p1hf,1,[]),nb,nb,nb,nb);<br>
p2hf = 2*p2hfpermute(p2hf,[1,3,2,4]);<br>
<br>
Roger Stafford

Fri, 02 Jul 2010 04:58:05 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759292
Zeeshan
Hi Roger <br>
<br>
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. <br>
<br>
Can you help me in this ?

Fri, 02 Jul 2010 05:21:08 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759299
Walter Roberson
"Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...<br>
> Hi Roger <br>
> <br>
> I want to plot a 2D mesh of spherical pore bodies connected with throats. <br>
<br>
Please do not add unrelated questions to an existing thread.

Fri, 02 Jul 2010 05:31:04 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759301
Roger Stafford
"Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...<br>
> Hi Roger <br>
> <br>
> 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. <br>
> <br>
> Can you help me in this ?<br>
         <br>
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.<br>
<br>
Roger Stafford

Fri, 02 Jul 2010 06:02:21 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759304
Zeeshan
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0jtio$agm$1@fred.mathworks.com>...<br>
> "Zeeshan " <zeeextra@yahoo.com> wrote in message <i0jrkt$44o$1@fred.mathworks.com>...<br>
> > Hi Roger <br>
> > <br>
> > 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. <br>
> > <br>
> > Can you help me in this ?<br>
>          <br>
> 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.<br>
<br>
Sorry if it bothers you <br>
Zeeshan

Fri, 02 Jul 2010 07:22:07 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759323
Volkan
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0jpbo$97t$1@fred.mathworks.com>...<br>
> "Volkan " <volkaned@hotmail.com> wrote in message <i0jemg$hka$1@fred.mathworks.com>...<br>
> > Hi,<br>
> > <br>
> > I am trying to avoid for loops but I couldn't find an easy solution for the problem below:<br>
> > <br>
> > p1hf = [1,2;3,4];<br>
> > nb = 2; % It might go up to 100 or so need a smarter solution<br>
> > p2hf = zeros(nb, nb, nb, nb);<br>
> > for a=1:nb<br>
> > for b=1:nb<br>
> > for c=1:nb<br>
> > for d=1:nb<br>
> > p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d)  p1hf(a,c)*p1hf(b,d);<br>
> > end<br>
> > end<br>
> > end<br>
> > end<br>
> > <br>
> > p2hf is 4D. Is there an easy way to avoid these loops?<br>
> > <br>
> > Your help will be greatly appreciated.<br>
>         <br>
> I think this does the same thing, but I'm not sure any time is saved.<br>
> <br>
> p2hf = reshape(reshape(p1hf,[],1)*reshape(p1hf,1,[]),nb,nb,nb,nb);<br>
> p2hf = 2*p2hfpermute(p2hf,[1,3,2,4]);<br>
> <br>
> Roger Stafford<br>
<br>
Thank you so much Roger. I think you are right about cpu usage. It doesn't really save time when nb > 50.<br>
<br>
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).<br>
<br>
Your solution is brilliant though. I gained a new perspective to consider when dealing with these kinds of problems. Thank you so much again.

Fri, 02 Jul 2010 07:52:05 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759331
Jan Simon
Dear Volkan,<br>
<br>
> p1hf = [1,2;3,4];<br>
> nb = 2; % It might go up to 100 or so need a smarter solution<br>
> p2hf = zeros(nb, nb, nb, nb);<br>
> for a=1:nb<br>
> for b=1:nb<br>
> for c=1:nb<br>
> for d=1:nb<br>
> p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d)  p1hf(a,c)*p1hf(b,d);<br>
> end<br>
> end<br>
> end<br>
> end<br>
<br>
Do you just want to omit the loops or do you need more speed?<br>
In the later case you could try this:<br>
<br>
p1hf = [1,2; 3,4];<br>
nb = 2; % It might go up to 100 or so need a smarter solution<br>
p2hf = zeros(nb, nb, nb, nb);<br>
for a = 1:nb<br>
for b = 1:nb<br>
t1 = 2*p1hf(a,b);<br>
for c = 1:nb<br>
p2hf(a,b,c, 1:nb) = t1*p1hf(c, 1:nb)  p1hf(a,c)*p1hf(b, 1:nb);<br>
end<br>
end<br>
end<br>
<br>
If the dimensions are matching, the "1:nb" index can be omitted:<br>
p2hf(a,b,c, :) = t1*p1hf(c, :)  p1hf(a,c)*p1hf(b, :);<br>
<br>
You can replace the loop over c also:<br>
...<br>
for a = 1:nb<br>
for b = 1:nb<br>
t1 = 2*p1hf(a,b);<br>
p2hf(a,b, :, :) = t1*p1hf  transpose(p1hf(a, :)) * p1hf(b, :);<br>
end<br>
end<br>
<br>
(Please test this  I do not have access to Matlab on this computer).<br>
<br>
Good luck, Jan

Fri, 02 Jul 2010 12:18:04 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#759385
Matt Fig
Try this:<br>
<br>
p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) ...<br>
bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));

Thu, 08 Jul 2010 00:36:03 +0000
Re: How to avoid nested for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285977#760728
Volkan
"Matt Fig" <spamanon@yahoo.com> wrote in message <i0klds$dl1$1@fred.mathworks.com>...<br>
> Try this:<br>
> <br>
> p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) ...<br>
> bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));<br>
<br>
Dear Matt,<br>
<br>
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.<br>
<br>
Thanks you all for your help.