Thread Subject: dynamically creating for loops

Subject: dynamically creating for loops

From: Ramana Murthy

Date: 9 Sep, 2009 06:20:17

Message: 1 of 6

Hi
I have a problem in creating for loops
Suppose there is a variable n.(say 10)

for i1 = 1:10
    for i2 = i1-1:i1+1
         for i3 = i2-1:i2+1
          .....
               for i'n'( i.e i10 in present case) = i9-1:i9+1
                statements

           end
         end
     end..
end

How can I fix this for loops varyingly ?
Is it possible at all ?

with regards,
ramana

Subject: dynamically creating for loops

From: Jan Simon

Date: 9 Sep, 2009 08:58:04

Message: 2 of 6

Dear Ramana murthy!

> I have a problem in creating for loops
> Suppose there is a variable n.(say 10)
>
> for i1 = 1:10
> for i2 = i1-1:i1+1
> for i3 = i2-1:i2+1
> .....
> for i'n'( i.e i10 in present case) = i9-1:i9+1
> statements
>
> end
> end
> end..
> end
>
> How can I fix this for loops varyingly ?
> Is it possible at all ?

It is possible! It looks like a perfect task for a recursive call of a subfunction.
If your statements depend on the variables of [i1 to i9], this vector must be one of the inputs of the function. Another input would be the parameter [n], which is e.g. reduced by 1 in each recursive call and if [n] equals 0, the statements are processed.

Of course, it is possible without recursion also using tricky constructions with a WHILE loop. Most likely the recursion is not the fastest method, but it can be really "nice".

Good luck, Jan

Subject: dynamically creating for loops

From: qooroo

Date: 9 Sep, 2009 09:07:04

Message: 3 of 6

of course if n > 500 then you'll get

Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.

I'd always avoid recursion if I could.

-qooroo

Subject: dynamically creating for loops

From: Bruno Luong

Date: 9 Sep, 2009 09:18:05

Message: 4 of 6

"qooroo " <qoorooAT@gmailDOT.comREMOVECAPS> wrote in message <h87r7o$jub$1@fred.mathworks.com>...
> of course if n > 500 then you'll get
>
> Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
> to change the limit. Be aware that exceeding your available stack space can
> crash MATLAB and/or your computer.

Unlikely, assuming each for-loop runs on 2 elements, how big is the number of combination before we run to recursive limit? 2^500 cases which is larger than 3e150 !

To OP, use NDGRID or any combination-generated tool in FEX to generate an array of combination your list of variables, then run a single for-loop on this array (or vectorize your code if you can.

Bruno

Subject: dynamically creating for loops

From: Ramana Murthy

Date: 9 Sep, 2009 09:37:03

Message: 5 of 6

Hi
Thank you for your directions.
to be more detail, I 'm having the following code

alpha =5;
M = 10
cnt = 1;
for j1 = 1 + alpha : M - alpha
    for j2 = max(alpha,j1-1) : min(j1+1,M - alpha)
        for j3 = max(alpha,j2-1) : min(j2+1,M - alpha)
            ......for j'N' =
            data(cnt,:) = [j1 j2 j3 .....];
            cnt = cnt+1;
            ....
        end
    end
end

Can someone help me with code.

with regards,
ramana

Subject: dynamically creating for loops

From: Ramana Murthy

Date: 9 Sep, 2009 09:40:04

Message: 6 of 6

Hi
Thank you for your directions.
to be more detail, I 'm having the following code

alpha =5;
M = 10
cnt = 1;
for j1 = 1 + alpha : M - alpha
    for j2 = max(alpha,j1-1) : min(j1+1,M - alpha)
        for j3 = max(alpha,j2-1) : min(j2+1,M - alpha)
            ......for j'N' =
            data(cnt,:) = [j1 j2 j3 .....];
            cnt = cnt+1;
            ....
        end
    end
end

Can someone help me with code.

with regards,
ramana

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
for Sprinceana 9 Sep, 2009 02:37:23
for loop Sprinceana 9 Sep, 2009 02:37:23
reference Sprinceana 9 Sep, 2009 02:37:23
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com