[英]For-Loop Definition in Matlab
I'm new to the Matlab. Can anyone explain for me why:我是 Matlab 的新手。任何人都可以为我解释为什么:
If a for loop starts with for i = 1:0, then the body can only execute once.
如果 for 循环以 for i = 1:0 开始,则循环体只能执行一次。
Thank you.谢谢你。
If you do not mention the step increment/decrement in for
loop in MATLAB by default it will always take it as increment by 1. For example默认情况下,MATLAB 中的
for
循环中如果没有提及步长递增/递减,它始终将其视为递增 1。例如
for i=1:3
disp(i)
end
It will print i
three times ie 1 2 3
它将打印
i
三次,即1 2 3
For below example it will print nothing as condition already becomes false对于下面的示例,它不会打印任何内容,因为条件已经变为 false
for i=1:0
disp(i)
end
For step more than 1 or negative increment (decrement) you have to mention it explicitly as shown below对于超过 1 的步骤或负增量(减量),您必须明确提及它,如下所示
for i=1:2:6
disp(i)
end
it will print 1 3 5
它将打印
1 3 5
or要么
for i=1:-1:0
disp(i)
end
It will print 1 0
它将打印
1 0
Try typing 1:0
in the command window. The result will be "1×0 empty double row vector", which means this is an empty vector.尝试在命令 window 中输入
1:0
。结果将是“1×0 empty double row vector”,这意味着这是一个空向量。 Perhaps what you were trying to achieve is the vector [1,0].也许您想要实现的是向量 [1,0]。 In this case, you should write explicitly
i = 1:-1:0
, which yields the correct result.在这种情况下,您应该明确地编写
i = 1:-1:0
,这会产生正确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.