在MATLAB中使用乘法步骤进行循环 [英] For loop with multiplication step in MATLAB
问题描述
是否可以通过自定义步骤在MATLAB中使用for循环?我想做的是迭代比给定数字小2的所有幂.例如,C ++中的等效循环为:
Is there any way to use a for-loop in MATLAB with a custom step? What I want to do is iterate over all powers of 2 lesser than a given number. The equivalent loop in C++ (for example) would be:
for (int i = 1; i < 65; i *= 2)
注释1:这是最适合for循环的一种迭代,因此我不想使用while循环.
注意2:我实际上是在使用Octave,而不是MATLAB.
Note 1: This is the kind of iteration that best fits for-loops, so I'd like to not use while-loops.
Note 2: I'm actually using Octave, not MATLAB.
推荐答案
也许您想要一些与
for i=2.^[1:6]
disp(i)
end
除非您需要弄清楚指数范围.这利用了以下事实:
a_(i+1) = a_i*2
可以重写为a_i = 2^i
.
Except you will need to figure out the range of exponents. This uses the fact that since
a_(i+1) = a_i*2
this can be rewritten as a_i = 2^i
.
否则,您可以执行以下操作
Otherwise you could do something like the following
i=1;
while i<65
i=i*2;
disp(i);
end
这篇关于在MATLAB中使用乘法步骤进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!