Clear Filters
Clear Filters

How can I get a fractional iteration value?

2 views (last 30 days)
Mahbubur Rahman
Mahbubur Rahman on 27 Feb 2016
Answered: Stephen23 on 27 Feb 2016
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)

Answers (1)

Stephen23
Stephen23 on 27 Feb 2016
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!