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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Ag. de 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 comentarios
Stephen23
Stephen23 el 29 de Ag. de 2017
Editada: Stephen23 el 29 de Ag. de 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 el 30 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

Star Strider
Star Strider el 19 de Ag. de 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 comentarios
Walter Roberson
Walter Roberson el 19 de Ag. de 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 el 20 de Ag. de 2017
Editada: Jan el 21 de Ag. de 2017
I prefer Walter's suggestion. It uses the faster "a:b" indexing in the for loop and allows to pre-allocate the output.

Iniciar sesión para comentar.


John BG
John BG el 19 de Ag. de 2017
Editada: John BG el 20 de Ag. de 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 comentarios
Gali Musa
Gali Musa el 30 de Ag. de 2017
i tried it but i noticed its still counting all the Pd. Could you please advice
Jan
Jan el 30 de Ag. de 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.

Iniciar sesión para comentar.


Jan
Jan el 30 de Ag. de 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.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by