|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
For Loop Index ProblemsHello,
I am having a simple problem with the for loop and its index. Basically, here is a simple program to illustrate the problem I am having. i=1; a=[1 2 3 8 8 8 3 2 1]; for i=1:length(a) i while a(1,i)==8 i=i+1; i end i i=i+1; i end This gives the output shown below: i = 1 i = 1 i = 2 i = 2 i = 2 i = 3 i = 3 i = 3 i = 4 i = 4 i = 5 i = 6 i = 7 i = 7 i = 8 i = 5 i = 6 i = 7 i = 7 i = 8 i = 6 i = 7 i = 7 i = 8 i = 7 i = 7 i = 8 i = 8 i = 8 i = 9 i = 9 i = 9 i = 10 So from here, we can see that... When a=8, the while loop is entered, and i equals 7 when it leaves the while loop... We then see that i=7 right outside of the while loop, and that after i=i+1, i now equals 8. But then, the next iteration of the for loop sets i equal to 5, which was the next iteration originally (before the while loop was entered). My question is... Is there any way to avoid the resetting of the index? Is there some sort of for loop where the user is allowed to control the index, and not the computer. If not, can anybody show me modifications to the code that would solve this problem? I cannot seem to figure it out on my own. Suggestions would be helpful too. Thank you, Joseph Gauthier |
|
|
Re: For Loop Index ProblemsI think you really want
while i < length(a), stuff inside your for loop, end Since you're already incrementing the "i" variable, you don't need the for loop to "push" you along. On Thu, Jun 4, 2009 at 2:40 PM, josgau33 <skankinkid33@...> wrote:
_______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: For Loop Index Problems I am having a simple problem with the for loop and its index. Basically,
here is a simple program to illustrate the problem I am having. i=1; a=[1 2 3 8 8 8 3 2 1]; for i=1:length(a) .... My question is... Is there any way to avoid the resetting of the index? Is there some sort of for loop where the user is allowed to control the index, Some techniques I'd recommend for debugging problems like that: - use single-line compound commands; they are much easier to manage using Octave's built-in command history (uparrow, edit as needed, re-execute). - use printf to better control printing, suppress automatic printing with ';' I rewrote your loop as: for i=1:length(a),printf("-----loop i=%d-----\n",i),while a(i)==8,i++;printf("-while i=%d\n",i),end,printf("---i is %d\n",i), i++;end with the following result: -----loop i=1----- ---i is 1 -----loop i=2----- ---i is 2 -----loop i=3----- ---i is 3 -----loop i=4----- -while i=5 -while i=6 -while i=7 ---i is 7 -----loop i=5----- -while i=6 -while i=7 ---i is 7 -----loop i=6----- -while i=7 ---i is 7 -----loop i=7----- ---i is 7 -----loop i=8----- ---i is 8 -----loop i=9----- ---i is 9 so, as you reported, the for loop resets the counter to its own sequence. If you want to change the index while looping, use while: i=1; while i<length(a),printf("-----loop i=%d-----\n",i),while a(i)==8,i++;printf("-while i=%d\n",i),end,printf("---i is %d\n",i), i++;end-----loop i=1----- ---i is 1 -----loop i=2----- ---i is 2 -----loop i=3----- ---i is 3 -----loop i=4----- -while i=5 -while i=6 -while i=7 ---i is 7 -----loop i=8----- ---i is 8 _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: For Loop Index ProblemsOn Jun 4, 2009, at 2:40 PM, josgau33 wrote:
> I am having a simple problem with the for loop and its index. > Basically, > here is a simple program to illustrate the problem I am having. > > i=1; > a=[1 2 3 8 8 8 3 2 1]; > > for i=1:length(a) > i > while a(1,i)==8 > i=i+1; > i > end > i > i=i+1; > i > end > > This gives the output shown below: > [...] > My question is... Is there any way to avoid the resetting of the > index? Is > there some sort of for loop where the user is allowed to control > the index, > and not the computer. If not, can anybody show me modifications to > the code > that would solve this problem? I cannot seem to figure it out on my > own. > Suggestions would be helpful too. Thank you, Octave's "for" is more like a "foreach" in other languages: at the beginning of each loop the next element from the list is copied into the loop variable. Compare these two one-liners for i = 1:10; printf("%2d %2d ", i, (i += 3) ); i, endfor i = 1; while i < 10; printf("%2d %2d ", i, (i += 3) ); i, endwhile If you want to control the loop variable use a while loop. Rob -- Rob Mahurin Department of Physics and Astronomy University of Tennessee 865 207 2594 Knoxville, TN 37996 rob@... _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: For Loop Index ProblemsThat fixed it! Thanks a lot guys :-)
|
| Free embeddable forum powered by Nabble | Forum Help |