Azzera filtri
Azzera filtri

How can i use interval with decimal at the end of a count in for loop. e.g. t=1:1:18.7

1 visualizzazione (ultimi 30 giorni)
How can i use interval with decimal at the end of a count in for loop. e.g. t=1:1:18.7
intervals = 18.7;
for t=1:intervals;
x(t+1)=Pd*t;
end

Risposta accettata

Walter Roberson
Walter Roberson il 19 Ago 2017
Your existing code works, executing with t set to 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, and 18. Then adding 1 to that would exceed 18.7 so there are no more iterations. The behavior is well defined.
What you need to watch out for is that if you are using a non-integer increment: https://www.mathworks.com/help/matlab/ref/colon.html
"x = j:i:k creates a regularly-spaced vector x using i as the increment between elements. The vector elements are roughly equal to [j,j+i,j+2*i,...,j+m*i] where m = fix((k-j)/i). However, if i is not an integer, then floating point arithmetic plays a role in determining whether colon includes the endpoint k in the vector, since k might not be exactly equal to j+m*i"
  6 Commenti
Stephen23
Stephen23 il 29 Ago 2017
Modificato: Stephen23 il 29 Ago 2017
"From your quote of the documentation, I would expect 0:0.01:1 to end at approximately 0.99 and not 1."
Why? 0.1 is represented by a binary value slightly larger than 0.1:
>> sprintf('%.20f\n',0.1)
ans = 0.10000000000000000555
and so it makes perfect sense that the end point would include 1, even taking into account floating point error. The whole point is that floating point error should not be relied up to behave in particular ways (e.g. always rounding down, as you assumed). Especially considering that calculations may also use guard digits, and different platforms may returns different results of the same precision.
Jan
Jan il 30 Ago 2017
@Daniel: According to the documentation colon creates the 1st half by addition to the start value, and the 2nd half by subtracting from the final value. Therefore the last value is exactly the specified number. I think this is exactly defined and documented.

Accedi per commentare.

Più risposte (3)

Star Strider
Star Strider il 19 Ago 2017
It depends on what you want to do. The second value in your colon (link) operator syntax is the step-size. Your ‘t’ vector will go from 1 to 18 and stop, because the next step-size is 1, so the vector will not exceed the end value, 18.7.
Indices in MATLAB must be integers greater than zero.
I am not certain what you want to do. One option would be to use the linspace function to create the vector, then step through the vector’s length.
If ‘Pd’ is a scalar, this will work:
tv = linspace(1, 18.7, 20); % Create Vector Of Length ‘20’ From ‘1’ To ‘18.7’
intervals = 1:length(tv);
for t = intervals
x(t+1)=Pd*tv(t);
end
  2 Commenti
Walter Roberson
Walter Roberson il 19 Ago 2017
I often express this as the general framework
tvals = ..... %list of actual values
num_tvals = length(tvals);
output = zeros(1, num_tvals);
for tidx = 1 : num_tvals;
t = tvals(tidx);
...
output(tidx) = ...
end
Jan
Jan il 20 Ago 2017
Modificato: Jan il 21 Ago 2017
I prefer Walter's suggestion. It uses the faster "a:b" indexing in the for loop and allows to pre-allocate the output.

Accedi per commentare.


John BG
John BG il 19 Ago 2017
Modificato: John BG il 20 Ago 2017
Hi Gali Musa
May be you would like to consider using an additional reference vector
nt
nt contains t vector indices and it's used as the for counter, not t or intervals directly
N=20
t = linspace(1, 18.7, N);
nt=[1:1:N]; % reference vector
dt=abs(t(1)-t(2)) % basic interval
for k=nt
x(nt(k)+1)=Pd*t(nt(k));
end
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  16 Commenti
Gali Musa
Gali Musa il 30 Ago 2017
i tried it but i noticed its still counting all the Pd. Could you please advice
Jan
Jan il 30 Ago 2017
See my answer. You can find out more details by your own. Check the value of mod(Pd, 1), then try mod(Pd, 1) ~= 0. I assume you want: sum(mod(Pd, 1) == 0, not ~=. This is a tiny detail and with some own effort it should be possible to solve it by your own.

Accedi per commentare.


Jan
Jan il 30 Ago 2017
Get the number of integers in the vector Pd:
sum(mod(Pd, 1) == 0)
After all these comments and questions for clarifications I still do not know, if this is the actual question or not.

Community Treasure Hunt

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

Start Hunting!

Translated by