Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concurrent for loop over 2 variables

Is it possible in Octave to made 2 loops in the same time like :

(for i=0:10 && j=10:20)
  i;
  j;
  end
like image 477
liquid-snake Avatar asked Sep 20 '25 09:09

liquid-snake


1 Answers

If the loops are of the same length, then yes. Not well known is that for non-vectors, a for loop, loops over columns. So just place your vectors in a matrix, one row per variable:

for r = [0:10; 10:20]
  printf ("1st is %2i; 2nd is %2i\n", r(1), r(2));
endfor

which returns:

1st is  0; 2nd is 10
1st is  1; 2nd is 11
1st is  2; 2nd is 12
1st is  3; 2nd is 13
1st is  4; 2nd is 14
1st is  5; 2nd is 15
1st is  6; 2nd is 16
1st is  7; 2nd is 17
1st is  8; 2nd is 18
1st is  9; 2nd is 19
1st is 10; 2nd is 20
like image 65
carandraug Avatar answered Sep 22 '25 05:09

carandraug