for函数的用法(matlab)

摘自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已经定义成某一个值)
  1. for R = 1:N
    for C = 1:N
    A(R,C) = 1/(R+C-1);
    end
    end
  2. 增量为-0.1来循环
    for S = 1.0: -0.1: 0.0,
    do_some_task(S),
    end
  3. 假设E为N维向量
    for E = eye(N),
    do_some_task(E),
    end
  • 实际在for语句中,expression向量从来都不存在,在循环次数很多的时候,使用:表达更加方便有效。
  • 可以使用break语句来提前跳出for循环。
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值