问题描述
Q-从0:limit-1创建一个计数器"(例如,如果选择3,它将显示0,1,2).计数器的长度在程序中没有确定,应在运行时确定,并且输入可以互不相同
Q- Create a "counter" from 0:limit-1 (for example if you choose 3 it will display 0,1,2). The length of counter is not determined in the program and it should be determined when it is being run and the inputs can differ from each other
这是python上的解决方案,但我想在matlab上进行计算.我该怎么办?
this is the solution on python but i want to compute it on matlab. how do i do that?
for i in range(3):
print(3-i)
for s in range(3,-1,-1)
print s
所以答案是:
3
2
1
3
2
1
0
推荐答案
正如Dan在上面的评论中提示您的那样, Matlab的冒号运算符已经完成了您想要的操作.
As Dan hinted you in the comments above, the colon operator of Matlab already do what you want.
以下是与您的Python示例相对应的示例:
Here are examples corresponding to your Python example:
使用裸冒号运算符:
3:-1:0
给予
ans =
3 2 1 0
这是一个1 x 4行向量.
which is a 1 by 4 row vector.
您将获得与以下相同的结果:
You'll get the same result with:
limit = 3;
limit:-1:0
如果要以此为基础进行循环:
If you want to use this as a basis for a loop:
limit = 3;
for i = limit:-1:0
disp(i)
end
将输出:
3
2
1
0
通常,您可以这样做:
More generally you could do:
istart = 6;
istep = -2;
iend = 0;
for i = istart:istep:iend
disp(i)
end
给出:
6
4
2
0
这篇关于创建一个“计数器"在matlab上从0:limit-1开始.计数器的长度在程序中不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!