Recently Posted




Notice: SSI.php was unable to load a session! This may cause problems with logout and other functions - please make sure SSI.php is included before *anything* else in all your scripts! in /home/tutorial/public_html/mathforum/SSI.php on line 155

for

for Repeat a specific number of times.
The general form of a statement is:

for variable = expr, statement, …, statement END

The columns of the expression are stored one at a time in
the variable and then the following , up to the
END, are executed. The expression is often of the form X:Y,
in which case its columns are simply scalars. Some examples
(assume N has already been assigned a value).

View CodeMATLAB
1
2
3
4
5
 I = 1:N,
 J = 1:N,
A(I,J) = 1/(I+J-1);
END
END

for S = 1.0: -0.1: 0.0, END steps S with increments of -0.1
E = EYE(N), … END sets E to the unit N-vectors.

Long loops are more memory efficient when the colon expression appears
in the statement since the index vector is never created.

The BREAK statement can be used to terminate the loop prematurely.

See also if, while, switch, break, end.

Tags: , ,