# Creating L points with uniform spacing d?

6 views (last 30 days)
Davide Mori on 13 Dec 2020
Edited: John D'Errico on 13 Dec 2020
Hi everyone,
I want to create a vector of L points along the x-axis which have a uniform spacing d. I have done as follows:
L=32;
d = 0.063
px = linspace(-d*L/2,d*L/2,L)';
but when I check the distance among them it result 0.065, why this? Suggestion? Thank you!
EDIT: I've read the documentation and according to it the spacing is (x2-x1)/(n-1), so to obtain a correct spacing I should have 33 points instead of 32, including the zero among the points, but the fact is that I don't want the 0 in my vector, so I think that the problem should be the definition of x1 and x2..how can I resolve?
Ive J on 13 Dec 2020
Why don't you use colon then?
px = x1:0.065:x2; % no guarantee that x2 will be within px though

John D'Errico on 13 Dec 2020
Edited: John D'Errico on 13 Dec 2020
You want to create a vector x, that contains exactly L points, with a uniform spacing of d, and that starts at 0. Trivial. Yes, you could use linspace. But for that to work, you need to figure out what the end point should be. Even if you use colon, it may seem like you need to know the end point. But you don't!
That is, if you want a vector of exactly L points, then start by using colon to create a vector of integers, from 0 to L-1. Then just multiply the entire vector by d. Trivial.
L = 5;
d = 0.25;
x = (0:L-1)*d
x =
0 0.25 0.5 0.75 1
So EXACTLY 5 points, with a spacing of 0.25. Made simply. I never need to know what the last point will be to use linspace, because for this purpose, linspace is the wrong tool to use. You wanted to generate L points equally spaced. So the correct tool to use is colon.
Suppose you wanted some other start point than zero? Again, still trivial.
4 points, starting at 12, with a spacing of 0.5.
x0 = 12;
L = 4;
d = 0.5;
x = x0 + (0:L-1)*d
x =
12 12.5 13 13.5
If this is something you will do often, then just create a function that does it for you. For example, as a function handle, I would do this:
vecmaker = @(x0,L,d) x0 + (0:L-1)*d;
But you could also write it as an m-file, if this is something you do frequently. Personally, I see no real purpose, because it is so easy to perform the task using colon. But feel free. Were I to do this, I would probably write it like this (and hopefully, I would choose a more creative name):
function x = vecmaker(L,d,x0)
% return a row-vector of length L, starting at x0, with a stride of d
% usage: vec = vecmaker(L,d,x0)
%
% arguments:
% L - the length of the final vector
% d - the stride between vector elements
% x0 - (OPTIONAL) the first element in the sequence.
% Default value: x0 = 0
%
% Example usage:
% x = vecmaker(4,0.5,12)
% x =
% 12 12.5 13 13.5
%
% Author: John D'Errico
% Date 12/13/2020
if (nargin < 3) || isempty(x0)
x0 = 0;
end
% create the vector using colon, then scale and shift the elements.
x = x0 + (0:L-1)*d;
end
As you can see, I wrote this to allow you to not provide the start point, if that would commonly be 0 by default.
The virtue of a language like MATLAB, is when you see something that you wish to do often, the language is so easily extensible. If there is capability you find missing, then write it yourself. Create your own toolbox of tools that you will use. Put them in a separate directory on your search path, so now you can use these tools for any work you do.
John D'Errico on 13 Dec 2020
This may seem slightly more difficult, But really, not so.
x = -d*(L-1)/2 + (0:L-1)*d;
So I created the vector just as I did before. L points, from 0 to L-1. Multiply by the stride. Then recognize the vector wants to be centered at the origin, so perform the proper translation. In terms of the function I wrote, the call would be
x = vecmaker(L,d,-d*(L-1)/2);
It looks like the version you wrote in your second comment would do the same.

KSSV on 13 Dec 2020
L=32;
d = 0.063 ;
px = zeros(L,1) ;
px(1) = -d*L/2 ;
for i = 2:L
px(i) = px(i-1)+d ;
end
Ive J on 13 Dec 2020
L = 32;
d = 0.063;
px = -d*L/2:d:d*(L-1)-d*L/2;

R2020b

### Community Treasure Hunt

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

Start Hunting!