Monday 15 July 2013

Octave algorithm loop iteration -



Octave algorithm loop iteration -

can please tell me why octave algorithm not execute lastly iteration = 4 , j = 2? seems affected break status in inner for-loop should not impact lastly iteration.

x = [2, 3, 4]; h = [1, 2]; y = [0, 0, 0, 0]; = 1:4 j = 1:2 printf("i = %d, j = %d\n", i, j); if((i-j < 0) || (i-j > 2)) break; endif y(i) = y(i) + h(j) * x(i-j+1); endfor endfor

i've tested on debian scheme , stops @ = 4 , j = 1. output is:

i = 1, j = 1 = 1, j = 2 = 2, j = 1 = 2, j = 2 = 3, j = 1 = 3, j = 2 = 4, j = 1

what want is:

for = 1:4 j = 1:2 if ((0 <= i-j) && (i-j <= 2)) printf("i = %d, j = %d\n", i, j); y(i) = y(i) + h(j) * x(i-j+1); endif endfor endfor

or

for = 1:4 j = 1:2 if ((i-j < 0) || (i-j > 2)) continue; endif y(i) = y(i) + h(j) * x(i-j+1); printf("i = %d, j = %d\n", i, j); endfor endfor

in code, when i==4 , j==1 statement

if((i-j < 0) || (i-j > 2)) break; endif

will jumps out of innermost loop. outermost loop completed (i==4) , programs ends.

references:

continue statement break statement

for-loop octave break

No comments:

Post a Comment