How can I get a fractional iteration value?

3 ビュー (過去 30 日間)
Mahbubur Rahman
Mahbubur Rahman 2016 年 2 月 27 日
回答済み: Stephen23 2016 年 2 月 27 日
If you look at this code - the number of iteration n is the iteration counter but for me it is also the seconds of the system (with max time 8 sec). I need the n (or the time in sec, which can be fractional) for which ts = 1000 inside the loop. Please give some ideas.
a = 5;
b = 22;
c = 13;
d = 4;
ts = 50+ 4*b;
stamp = zeros(1,8);
for n = 1:8
ta = 1+(b+a)/n*c;
ts = ta +ts;
b = b+1;
a = a + b/2;
c = c-.5;
stamp(n) = ts;
end
disp(d)
disp(a)

回答 (1 件)

Stephen23
Stephen23 2016 年 2 月 27 日
You can iterate over fractional values:
for k = 1./(2:5)
disp(k)
end
However if you are using the k values as indices this is not possible. In this case you have two possibilities:
1) define a vector before the loop, and index into this:
V = 1./(2:5);
for k = 1:numel(V)
disp(V(k))
... code using k as an index
end
2) calculate the fractional value from k
for k = 1:4
disp(1/(k+1))
... code using k as an index
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by