DOI link for Control Statements
Control Statements book
The expression is a matrix, and the statement group X is repeated as many times as the number of elements in the columns of the expression matrix. The index takes on the elemental values in the matrix expression. Usually, the expression is something like
m:n or m:i:n
where m is the beginning value, n the ending value, and i is the increment. Suppose we would like to find the squares of all the integers from 1 to
100. We could use the following statements to solve the problem:
sum = 0; for i = 1:100 sum = sum + i^2; end sum
For loops can be nested, and it is recommended that the loop be indented for readability. Suppose we want to fill a 10-by-20 matrix, b, with an element value equal to unity. The following statements can be used to perform the operation.