Truncate matrix elements to a particular length

Ok. I am revising my previous question. I have two matrices that have the X and Y coordinates of a trajectory.
X= [ 0 1.1 1.9 2.6 3.01 4.56 5.04]
Y= [ 0 -0.013 0.008 0.008 -0.002 0.034 0.08]
So the trajectory comprises of 7 points and 6 segments. The length of each subsegment is given by the following matrix. The length is calculated by the distance formula
S=[ 1.1001 0.8003 0.7 0.4101 1.5504 0.4822]
The sum of all these subsegments equals sum(S)=5.0431. This is the length of my trajectory. To do further analysis, I want all my trajectories to have the same length (say 5 in this case). I want to interpolate the last point so that it falls on the trajectory and allows the trajectory to have a predefined length. Only the last point should be interpolated.
So my final output should look like this
X= [ 0 1.1 1.9 2.6 3.01 4.56 Xi]
Y= [ 0 -0.013 0.008 0.008 -0.002 0.034 Yi]
I need to find Xi and Yi using interpolation (interp1 probably) so that sum(S) becomes 5.
Can anyone help me write a code for this.
Thanks, Nancy

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 12 de Dic. de 2011
xy = [X;Y];
d = diff(xy(:,end-[1 0]),1,2);
k = 5 - sum(sqrt(sum(diff(xy(:,1:end-1),1,2).^2)))
xy(:,end)= k/sqrt(sum(d.^2))*d+xy(:,end-1)
OR
xy = [X;Y]
d = diff(xy,1,2)
D = hypot(d(1,:),d(2,:))
k = 5 - sum(D(1:end-1))
XYi = k/D(end)*d(:,end) + xy(:,end-1)

5 comentarios

Nancy
Nancy el 13 de Dic. de 2011
Thanks Andrei. What is d in the third line of the first solution. Is it the same as the one in the second solution.
And if possible could you explain your logic?
Andrei Bobrov
Andrei Bobrov el 13 de Dic. de 2011
Hi Nancy! My typo! Corrected.
Nancy
Nancy el 13 de Dic. de 2011
Could you explain your logic?
Sean de Wolski
Sean de Wolski el 13 de Dic. de 2011
xy vertically concatenates x,y
d = is the horizontal numerical first derivative, e.g. a(i+1)-a(i)
D is the sqrt sum squares (doc hypot) of x and y
k is 5 minus the sum of all hypotenuses except the last
XYi is something.
Nancy
Nancy el 13 de Dic. de 2011
Thanks Sean. I need the logic behind XYi.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Dic. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by