循环中的迭代次数 [英] Number of iterations in a loop

查看:129
本文介绍了循环中的迭代次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码

for a=1:5:100
    a = a+ 1;
end

重复20次?

a在每次迭代中增加5,但在实际循环中也增加1. 99/6 = 16.5或17次迭代,那么为什么要执行20次?

a goes up by 5 every iteration, but also goes up by 1 in the actual loop. 99/6 = 16.5 or 17 iterations, so why does it do 20?

感谢您帮助了解for循环功能的工作原理.

Thanks for any help understanding how the for loop function works.

推荐答案

在Matlab中,对for循环内的循环索引变量(a)所做的任何操作都将被丢弃,并且a会在下一遍的开始.因此循环内的a = a + 1无效.参见

In Matlab, whatever you do to the loop index variable (a) inside a for loop is thrown away, and a gets reset at the beginning of the next pass. So the a = a + 1 inside the loop has no effect. See Is there a foreach in MATLAB? If so, how does it behave if the underlying data changes?.

这篇关于循环中的迭代次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆