Get distance between adjacent array cells

2 次查看(过去 30 天)
Hello everyone,
I have a little problem calculating a velocity from a distance matrix. I do have the time array already.
My distance matrix looks as following:
dist=[x_coord1, y_coord1;
x_coord2, y_coord2;
....];
The coordinates are absolute, and I need the relative distance between the current coordinates and the previous one.
So, my vector should look like this:
newdist=[pdist2([x_coord2,y_coord2],[x_coord1,y_coord1]);
pdist2([x_coord3,y_coord3],[x_coord2,y_coord2])
......];
But instead pdist2 gives a vector with the distance of every possible combination of cells.
My questions(in my opinion at least):
1. Are there any functions which give me what I want immediately; or if this is not the case
2. Is there a way to decipher the pdist2 vector and get the correct values?
Thanks for reading this long post and helping :)
Kind regards, Dries van Roovert

采纳的回答

Dries van Roovert
Dries van Roovert 2016-11-16
For anyone interested in the answer:
PairwiseDist=squareform(pdist(dist,'euclidean'));
RelativeDist=diag(PairwiseDist,-1);
You'll get the distance from coord 2 to 1, coord 3 to 2, ... and so on in a vector.
Cheers

更多回答(1 个)

Jan
Jan 2016-11-14
编辑:Jan 2016-11-14
v = diff(dist, 1, 1);
Perhaps:
v = sqrt(sum(v .* v, 2));
  2 个评论
Dries van Roovert
Dries van Roovert 2016-11-14
I need the euclidean distance between the [x1,y1] and [x2,y2]. not just the distance between x1 & x2 and y1 & y2.
Dries van Roovert
Dries van Roovert 2016-11-15
That's not it either, on every row of the solution array should be the distance between the current coordinates and the previous.
%pdist2 is a function calculating distance between data sets
row 1 = pdist2([x2,y2],[x1,y1],'euclidean')
row 2 = pdist2([x3,y3],[x2,y2],'euclidean')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by