摘自matlab
for Repeat statements a specific number of times.
The general form of a for 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 statements, 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).
for R = 1:N
for C = 1:N
A(R,C) = 1/(R+C-1);
end
end
Step S with increments of -0.1
for S = 1.0: -0.1: 0.0, do_some_task(S), end
Set E to the unit N-vectors
for E = eye(N), do_some_task(E), end
Long loops are more memory efficient when the colon expression appears
in the for statement since the index vector is never created.
The BREAK statement can be used to terminate the loop prematurely.
用法(翻译)
for:重复一个语句特定的次数。
for语句的一般格式是:
for variable = expr, statement, …, statement END
这个expression每次都会在variable中存一次,接着执行下面的语句,直到for当次循环结束,继续执行下一次。expression一般的表达形式是X:Y,表示从x>>y,循环Y-X次,在这种情况下它的列只是简单的标量。
- 下面是一些例子(假设N已经定义成某一个值)
- for R = 1:N
for C = 1:N
A(R,C) = 1/(R+C-1);
end
end - 增量为-0.1来循环
for S = 1.0: -0.1: 0.0,
do_some_task(S),
end - 假设E为N维向量
for E = eye(N),
do_some_task(E),
end
- 实际在for语句中,expression向量从来都不存在,在循环次数很多的时候,使用:表达更加方便有效。
- 可以使用break语句来提前跳出for循环。