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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2017-8-19
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 个评论
Stephen23
Stephen23 2017-8-29
编辑:Stephen23 2017-8-29
"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 2017-8-30
@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.

请先登录,再进行评论。

更多回答(3 个)

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

请先登录,再进行评论。


John BG
John BG 2017-8-19
编辑:John BG 2017-8-20
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 个评论
Jan
Jan 2017-8-30
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.

请先登录,再进行评论。


Jan
Jan 2017-8-30
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by