linspacewritten to Matlab (i.e. it is not a built-in function). You can easily change its code so as not to generate the last element, and save it as another function.
In fact, if you see the code linspace, you will notice that the last item needs to be added specifically. So your guess was correct: it is more "natural" so as not to include the last element.
In the code below, I include the modified three lines and the original ones for comparison.
function y = linspace2(d1, d2, n)
if nargin == 2
n = 100;
end
n = double(n+1); %// modified line
%// Originally: n = double(n);
n1 = floor(n)-1;
vec = 0:n-2;
if isinf(d2 - d1)
y = d1 + (d2/n1).*vec - (d1/n1).*vec; %// modified line
%// Originally: y = [d1 + (d2/n1).*vec - (d1/n1).*vec, d2];
else
y = d1 + (vec.*(d2-d1)/n1); %// modified line
%// Originally: y = [d1 + (vec.*(d2-d1)/n1), d2];
end
source
share