How can I get a fractional iteration value?
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!