matlab - Calculate great circle distance of all matrix elements to all matrix elements -
i have following vector of coordinates
ll = [lat lon]; % 5000 x 2
and want calculate distances of elements elements using:
[dist, ~] = deg2km(distance[lat1, lon1, lat2, lon2]);
so final matrix be, guess, 5000x5000. btw function distance
comes in mapping toolbox. tried loop one-by-one, takes centuries run.
update
ll = -79.49583374 40.029166991 -79.50416707 40.029166991 -79.50416707 40.037500324 d = pdist(ll, @(x2,x1)(deg2km(distance(x1(1),x1(2),x2(1),x2(2))))) d = 2014.58795578131 2014.58795578131 0.168797611011563
while first 2 coordinates:
d = deg2km(distance(ll(1,2),ll(1,1),ll(2,2),ll(2,1))) d = 0.709531880098433
----
any please?
i didn't realise distance
matlab function, sorry. try this:
d = pdist(ll, @(x1,x2) (distance(x1(:,1),x1(:,2),x2(:,1),x2(:,2))))
what @(x1,x2) (distance(x1(:,1),x1(:,2),x2(:,1),x2(:,2)))
do?
it's anonymous function expects 2 inputs, x1
, x2
, , each should n-by-2. let's pretend n equals 1. pdist
give our anonymous function every different pair of rows ll
inputs x1
, x2
. row ll
of form [lat, lon]
, getting 2 such rows. know distance
requires distance(lat1,lon1,lat2,lon2)
. if x1
row of ll
it's [lat1, lon1]
x1(1)
lat1
, x1(2)
lon2
. same x2
, [lat2,lon2]
.
so our anonymous function doing converting distance
function taking 4 inputs, more standard formed matlab distance function accepts 2 inputs.
edit:
as swapping latitudes , longitudes, might work you:
d = pdist(ll, @(x1,x2) (distance(x1(:,2),x1(:,1),x2(:,2),x2(:,1))))
Comments
Post a Comment