MATLAB Functions | Help Desk |
for
Repeat statements a specific number of times
forThe general format isvariable
=expression
statements
end
forThe columns of thevariable = expression
statement
...statement
end
expression
are stored one at a time in the variable while the following statements, up to the end
, are executed.
In practice, the expression
is almost always of the form scalar
: scalar
, in which case its columns are simply scalars.
The scope of the for
statement is always terminated with a matching end
.
Assume n
has already been assigned a value. Create the Hilbert matrix, using zeros
to preallocate the matrix to conserve memory:
a = zeros(n,n) % Preallocate matrix for i = 1:n for j = 1:n a(i,j) = 1/(i+j -1); end endStep
s
with increments of -0.1
for s = 1.0: -0.1: 0.0Successively set,
...,
end
e
to the unit n
-vectors:
for e = eye(n)The line,
...,
end
for V = Ahas the same effect as,
...,
end
for j = 1:nexcept,
V = A(:,
j);...,
end
j
is also set here.
break
Break out of flow control structures
end
Terminate for
, while
, switch
, and if
statements and
indicate the last index
if
Conditionally execute statements
return
Return to the invoking function
switch
Switch among several cases based on expression
while
Repeat statements an indefinite number of times