for loop是一个重复控制结构,它允许您高效地编写需要执行特定次数的循环。
matlab中afor循环的语法是-
for index=values <program statements> ... end
值具有以下表单之一-
Sr.No. | Format & 描述 |
---|---|
1 | initval:endval 将索引变量从initval递增1到endval,并重复执行程序语句,直到index大于endval。 |
2 | initval:step:endval 每次迭代时按值step递增index,或在step为负数时递减。 |
3 | valArray 在每次迭代上从数组valArray的后续列创建列向量索引。例如,在第一次迭代中,index=valArray(:,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
创建脚本文件并键入以下代码-
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
创建脚本文件并键入以下代码-
for a=[24,18,17,23,28] disp(a) end
运行文件时,它将显示以下结果-
24 18 17 23 28
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)