for
loop to repeat specified number
of times
forindex
=values
statements
end
for
executes a group of statements in a loop for a specified
number of times. index
= values
, statements
,
endvalues
has one of the
following forms:
initVal
:endVal
—
Increment the index
variable from initVal
to endVal
by 1
,
and repeat execution of statements
until index
is
greater than endVal
.
initVal
:step
:endVal
—
Increment index
by the value step
on
each iteration, or decrements index
when step
is
negative.
valArray
— Create
a column vector, index
, from subsequent
columns of array valArray
on each iteration.
For example, on the first iteration,
.
The loop executes a maximum of index
= valArray
(:,1)n
times,
where n
is the number of columns of valArray
,
given by numel(
.
The input valArray
(1,:))valArray
can be of any MATLAB® data
type, including a character vector, cell array, or struct.
To programmatically exit the loop, use a break
statement. To skip the rest of
the instructions in the loop and begin the next iteration, use a continue
statement.
Avoid assigning a value to the index
variable
within the loop statements. The for
statement overrides
any changes made to index
within the loop.
To iterate over the values of a single column vector, first transpose it to create a row vector.