help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple (and probably stupid) question about for loops


From: CdeMills
Subject: Re: Simple (and probably stupid) question about for loops
Date: Fri, 28 May 2010 02:54:31 -0700 (PDT)


AlbFrigerio wrote:
> 
> OK, thanks everybody for your replies. Maynard, you are completely right :
> I was just thinking in C , not in Octave !!! It doesn't matter, I'll use
> another way to solve the problem.
> 
> Dear Andy, your idea is very good, but my example was very stupid. In my
> real problem I don't know a priori which would be the values of the
> counter to be skipped, I'll found it in the loop itself. Hence the while
> statement is good (it's my own idea) while the for loop on [1,2...] won't
> work.
> 
> 
There are many ways to do that:
1) place a test inside the loop to execute instructions based upon some
conditional:
    for k=1:10
      if k < 3 || k > 4 
        k
     endif
    endfor
2) use the "continue" or "break" to skip parts of the loop
    for k=1:10
      if k> 2 && k < 5 continue; endif
      k
    endfor
3) vectorise the loop
    myloop =  (1:10); myloop(myloop > 2 & myloop < 5) = []; %# remove some
elems
    for k = myloop,
      k
    endfor

Regards

Pascal
-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/Simple-and-probably-stupid-question-about-for-loops-tp2230002p2234351.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]