Azzera filtri
Azzera filtri

Why linspace function and Colon (:) operator giving me different number of elements in creating a vector ?

21 visualizzazioni (ultimi 30 giorni)
I am using an Optimization algorithm and the lower and upperbound for time is in between 3.8 seconds to 6 seconds. I wanted to create only 10 element from StartTime to EndTime. Moreover, I wanted to know the successive difference between the element as I will use it later in my function.
StartTime = 3.81;
EndTime = 5.91612005426755; % random number generated from optimization algorithm
tvec = linspace(StartTime, EndTime, 10)
tvec = 1×10
3.8100 4.0440 4.2780 4.5120 4.7461 4.9801 5.2141 5.4481 5.6821 5.9161
Dtime = tvec(2)-tvec(1)
Dtime = 0.2340
% If I use Dtime to create the same vector, I am getting only 9 element
% this time.
StartTime:Dtime:EndTime
ans = 1×10
3.8100 4.0440 4.2780 4.5120 4.7461 4.9801 5.2141 5.4481 5.6821 5.9161
Linspace Vs Colon

Risposta accettata

Image Analyst
Image Analyst il 26 Dic 2021
linspace() gives you exactly 10 elements and scales the numbers to make sure that's true.
Colon steps along until the number is more than the ending number and quits without exceeding the ending number. So the 10th number is slightly more than 5.91612005426755 so it quits at 5.6821. So there will be a variable numbers and sometimes a little less than what you expected.
See the FAQ for why sometimes numbers don't add to what you think they should.
  3 Commenti
Image Analyst
Image Analyst il 27 Dic 2021
For me, and online, I always get 10 (see below where I ran it from my browser)
StartTime = 3.81;
EndTime = 5.91612005426755; % random number generated from optimization algorithm
tvec = linspace(StartTime, EndTime, 10)
tvec = 1×10
3.8100 4.0440 4.2780 4.5120 4.7461 4.9801 5.2141 5.4481 5.6821 5.9161
Dtime = tvec(2)-tvec(1)
Dtime = 0.2340
% If I use Dtime to create the same vector, I am getting only 9 element
% this time.
t2 = StartTime : Dtime : EndTime
t2 = 1×10
3.8100 4.0440 4.2780 4.5120 4.7461 4.9801 5.2141 5.4481 5.6821 5.9161
I guess if you're still getting 9 elements, you could go a fraction of a Dtime past the end point:
t3 = StartTime : Dtime : (EndTime + 0.1 * Dtime)
t3 = 1×10
3.8100 4.0440 4.2780 4.5120 4.7461 4.9801 5.2141 5.4481 5.6821 5.9161

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 26 Dic 2021
Modificato: Matt J il 27 Dic 2021
The algorithm that linspace uses is,
function out=linspace(a,b,n)
increment=(b-a)/(n-1);
for i=1:n
out(i)=a + increment*(i-1);
end
end
The algorithm that colon uses is,
function out=colon(a,increment,b)
n=floor((b-a)/increment)+1;
out=linspace(a, a+(n-1)*increment, n);
end
In the case of colon, n must be computed from the inputs using floating point arithemtic. In your case, (b-a)/increm=9 in ideal math, but in floating point it can work out to 8.9999999... leading to floor((b-a)/increm)=8. When this happens, n will of course be one fewer than it should be in ideal math.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by