Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: dynamically creating for loops
Date: Wed, 9 Sep 2009 08:58:04 +0000 (UTC)
Organization: Universit&#228;t Heidelberg
Lines: 26
Message-ID: <h87qms$a3o$1@fred.mathworks.com>
References: <h87hf1$q8e$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1252486684 10360 172.30.248.37 (9 Sep 2009 08:58:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 9 Sep 2009 08:58:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869888
Xref: news.mathworks.com comp.soft-sys.matlab:569181


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