0
votes

I am trying to find out the distance between two list of locations(lat,long). I have them all stored in separate arrays lat1, lon1, lat2, lon2. The first set is the ones I am reading of a gps system and the second is a reference. They both have over 1000 rows.

What I want to do is. Calculate distance between lat1, lon1 for all points of lat2, lon2 Then move to 2nd row of lat1, lon1 again calculate for all rows of lat2, lon2 and keep repeating.

So every calculation of the lat1, lon1 should be performed for every point of lat2, lon2.

I have to finally take a decision based on the output distance being <4

Could someone help with this please. Really appreciate this.

1

1 Answers

1
votes

Hopefully you have the stats toolbox because then you can just use pdist2 for this:

pdist2([lat1, lon1], [lat2, lon2])

If you don't have the stats toolbox:

%// Simple example data
a = [0, 0; 0, 1; 1, 1];
b = [0, 0; 0, 1; 1, 1];

n = size(a,1);

[X, Y] = ndgrid(1:n,1:n)
dist = sqrt(sum((a(X(:),:) - b(Y(:),:)).^ 2, 2))

reshape(dist, n, n)

results (correctly) in:

ans =

   0.00000   1.00000   1.41421
   1.00000   0.00000   1.00000
   1.41421   1.00000   0.00000