


This can be extended to loops over multi-dimensional arrays. So the first loop iterates twice, producing two column vectors, followed by, and likewise for the loop over the cell array. In this case the variable i takes on the value of the columns of the matrix or cell matrix. Within Octave is it also possible to iterate over matrices or cell arrays using the for statement. This process continues until there are no more elements to assign. When the end of the loop body is reached, the next value in the range is assigned to the variable i, and the loop body is executed again. Then the variable i is assigned the first element of the range and the body of the loop is executed once.
#OCTAVE FOR LOOP CODE#
This code works by first evaluating the expression 3:10, to produce a range of values from 3 to 10 inclusive. The following example shows another way to create a vector containing the first ten elements of the Fibonacci sequence, this time using the for statement: If var is a column vector or a matrix, var will be a column vector each time the loop body is executed. If expression is a range, a row vector, or a scalar, the value of var will be a scalar each time the loop body is executed.

Instead of assigning the complete result of the expression, it assigns each column of the expression to var in turn. The assignment expression in the for statement works a bit differently than Octave’s normal assignment statement. If the value of expression is a structure, var may also be a vector with two elements. Usually it is a simple variable name or an indexed variable. Where body stands for any statement or list of statements, expression is any valid expression, and var may take several forms. The general form of the for statement looks like this: for var = expression The for statement makes it more convenient to count iterations of a loop.
