-2

I have two sets of x,y data. The plot of those data can be seen in figure below:

enter image description here

Let say, if the blue line is the reference, how can I calculate the distance between the blue line and the red line? To make it clearer, if the red line is behind the blue line, the difference will be negative and vice versa.

I've tried using pdist, but I think this is not the solution I want.

Any help will be really appreciated. Thank you

user3006147
  • 81
  • 1
  • 6
  • 4
    are the lines shown here meant to be representations of paths one would be able to walk? And do you want the distance between any two points at a given 'time' along this route? Because otherwise the 'distance' you are talking about is very ill-defined: the red line actually loops back and forth in x value around the 1060 mark, so you can't really say a signal is lagging or leading by a set amount. – Wouter Kuijsters Mar 07 '15 at 16:53
  • You could also compute the distance of the closest point of the other curve using `knnsearch`, but what exactly you mean by positive or negative distance is unclear. Imagine a circle of radius `1` and a circle of radius `2` with the same center, where would you define the distances positive and where negative? – knedlsepp Mar 07 '15 at 19:03
  • @ Wouter Kuijsters: No those lines are not path. Lines represent zero contour of topography (x y z=0). So I want to know the displacement between the blue lines and the red lines. If the red line located behind the blue line, then value will be negative and if the red line in front of the blue line, the difference will be positive. The distance is normal from the blue line. – user3006147 Mar 07 '15 at 19:25
  • 1
    @user3006147: This is completely different story ! You should have told from the start that you want the distances to be computed normally to the blue curve. – Ratbert Mar 07 '15 at 19:48
  • Important point: your two curves seem to converge at both extremities. Is it something we can safely assume or is it just a coincidence ? It seems to me that if this is not the case your problem is ill-defined. – Ratbert Mar 07 '15 at 20:08
  • @Crazy Rat: Yes we can assume that curves converge at he both extremities. Is it possible to solve this problem? – user3006147 Mar 07 '15 at 21:28

1 Answers1

4

Computing the Euclidean distance between the trajectories a each time step is very simple, the difficult part is to sign this distance. One possibility is then to use the cross product.

Here is my code:

% --- Define and display sample curves
x1 = cumsum(rand(100,1)-0.3);
y1 = cumsum(rand(100,1)-0.3);

x2 = cumsum(rand(100,1)-0.3);
y2 = cumsum(rand(100,1)-0.3);

subplot(1,2,1)
plot(x1, y1, x2, y2)

% --- Get the distance
d = sqrt((x2-x1).^2 + (y2-y1).^2);

% --- Sign the distance with cross product
u = [x1(:) y1(:)];
u(:,3) = 0;

v = [x2(:) y2(:)];
v(:,3) = 0;

tmp = cross(u, v);
d = d.*sign(tmp(:,3));

% --- Display the result
subplot(1,2,2)
plot(d)

which gives me the following result (right plot):

Result

Best,

Ratbert
  • 5,463
  • 2
  • 18
  • 37