Concatenate matrix numbers linspace

Hi there,
I have a matrix variable x = [0 1 2 3]
I want to generates linearly spaced vectors in between the numbers into a variable. My problem here is concatenate the numbers into p the next time n increases.
I know i should be using linspace to generate number for eg:
for i = 1:(length(x)-1)
p = linspace(x(i),x(i+1),0.5)
end
the results i want is:
p = 0 0.5 1 1.5 2 2.5 3
Hope someone can shed some light here.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 13 Août 2012
Modifié(e) : Azzi Abdelmalek le 29 Août 2012
try this
x = [0.25 1 1.5 2 2.4 2.6]
x=unique(sort([x x(1:length(x)-1)+diff(x)/2]) )
if you want put 2^n-1 samples between each value use this function
function y=linspace_n(x,n)
for k=1:n
x=unique(sort([x x(1:length(x)-1)+diff(x)/2]) )
end
y=x

6 commentaires

Sean de Wolski
Sean de Wolski le 14 Août 2012
This won't work if you have duplicate values in x.
Azzi Abdelmalek
Azzi Abdelmalek le 14 Août 2012
yes Wolski, but i have used "unique" to remove duplication
Sean de Wolski
Sean de Wolski le 14 Août 2012
I get that. But your answer will fail with x = [0 1 2 3 2 1 0]; where x has unique values!
Matt Fig
Matt Fig le 14 Août 2012
Modifié(e) : Matt Fig le 14 Août 2012
Regardless, this function does not do what you say it does, Azzi.
>> x = [1 2 3];y = linspace_n(x,3).' % 3 points between each?
y =
1.0000
1.1250
1.2500
1.3750
1.5000
1.6250
1.7500
1.8750
2.0000
2.1250
2.2500
2.3750
2.5000
2.6250
2.7500
2.8750
3.0000
Matt Fig
Matt Fig le 14 Août 2012
Modifié(e) : Matt Fig le 14 Août 2012
Better to use one of these. First a vectorized version:
function y = linspace_n2(x,N)
% Puts N values linear interpolated between each element of x.
% Author - Matt Fig
L = length(x);
y = bsxfun(@plus,bsxfun(@times,(0:N)',diff(x)/(N+1)),x(1:L-1));
y = [reshape(y,1,(L-1)*(N+1)),x(L)];
Or one could even go with a simplistic:
function y = linspace_n3(x,N)
% Puts N values linear interpolated between each element of x.
% Author - Matt Fig
y = [];
R = (0:N);
D = diff(x)/(N+1);
for ii = 1:length(x)-1
y = [y x(ii) + R.*D(ii)];
end
y = [y x(end)];
Azzi Abdelmalek
Azzi Abdelmalek le 29 Août 2012
sorry, did'nt read your comment, yes what it does is puting 2^n-1 points instead of n

Connectez-vous pour commenter.

Plus de réponses (5)

Matt Fig
Matt Fig le 13 Août 2012
Modifié(e) : Matt Fig le 13 Août 2012
p = 0:.5:3;
or
p = linspace(0,3,7)
EDIT.
I think I misunderstood your problem. Do you mean like this:
x = [0.25 1 1.5 2 2.4 2.6]
x(2,1:end-1) = diff(x)/2+x(1:end-1);
x = reshape(x(1:end-1),1,[])
Amazing Trans
Amazing Trans le 13 Août 2012

0 votes

Sorry, i had to be more clear. x is not linearly equal, which means x can be this as well
x = [0.25 1 1.5 2 2.4 2.6]
Thanks!
Sean de Wolski
Sean de Wolski le 13 Août 2012
Modifié(e) : Sean de Wolski le 13 Août 2012
Here is a terrible solution:
x = 0:3; %sample x
x = [x(1:end-1); x(2:end)]; %each start/end pair
nAdd = 2; %add two elements between
xnew = interp1((1:2)',x,linspace(1,2,2+nAdd)); %interpolate (2d linspace)
xnew = vertcat(reshape(xnew(1:end-1,:),[],1),x(end)) %keep non-duplicate parts
Amazing Trans
Amazing Trans le 14 Août 2012
Modifié(e) : Amazing Trans le 14 Août 2012
Alright here is something more challenging:
If i have x = [0 1 2 4 1]
I want x to be spaced equally n time. n = 2 therefore results = [0 0.25 0.5 1 1.25 1.5 2 2.6667 3.3333 4 3 2 1]
the other function i would like is increase by 0.1 or n where: x = [ 0 1 2] results = [0 0.1 0.2 0.3...0.9 1 1.1 1.2 1.3...1.9 2]
My first initial thought was to use linspace with for loop. and then results is a matrix where when the i increase it will concatenate into the results matrix... I'm not sure on the ??? part. The easier the code the better as I am transferring the matlab code to VB later as well. Of course this code below does not work for results(start).the error i get is: "Subscript indices must either be real positive integers or logicals." Any idea what to modify here?
for eg:
x = [0 3 1]
start = 0;
for i = 1:(length(x)-1)
y = linspace(x(i), x(i+1),points);
results(start) = y
start = start + points + 2;
end

3 commentaires

I guess i will do this for n points in between x1 & x2:
start = 0;
for i = 1: (length(x)-1)
y=linspace(x(i),x(i+1),n+2)
for j = 1:length(y)
results(start+j) = y(j)
end
start = start +length(y) -1
end
Matt Fig
Matt Fig le 14 Août 2012
Please close out this question by selecting a best answer then post a new question and link back to this one.
Sean de Wolski
Sean de Wolski le 14 Août 2012
My answer handles your first scenario!

Connectez-vous pour commenter.

Amazing Trans
Amazing Trans le 14 Août 2012

0 votes

Thanks very much everybody. Problem solved.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by