for 循环 是一种重复控制结构,使您可以有效地编写需要执行特定次数的循环。
语法
MATLAB中for循环的语法为:
for index = values <program statements> ... end
值 (values)具有以下形式之一-
实例1
创建一个脚本文件并输入以下代码-
for a = 10:20 fprintf('value of a: %d\n', a); end
运行文件时,它显示以下结果-
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 value of a: 20
实例2
for a = 1.0: -0.1: 0.0 disp(a) end
1 0.90000 0.80000 0.70000 0.60000 0.50000 0.40000 0.30000 0.20000 0.10000 0
实例3
for a = [24,18,17,23,28] disp(a) end
24 18 17 23 28