Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Aug. 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 Kommentare
Stephen23
Stephen23 am 29 Aug. 2017
Bearbeitet: Stephen23 am 29 Aug. 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 am 30 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

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

Melden Sie sich an, um zu kommentieren.


John BG
John BG am 19 Aug. 2017
Bearbeitet: John BG am 20 Aug. 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 Kommentare
Gali Musa
Gali Musa am 30 Aug. 2017
i tried it but i noticed its still counting all the Pd. Could you please advice
Jan
Jan am 30 Aug. 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.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 30 Aug. 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