User defined distance function

I'm try to calculate spherical distance between two points.
The formula is
D=R*arccos[cos(b1)*cos(b2)*cos(a1-a2)+sin(b1)*sin(b2)]
and two points' latitude is b1,b2 and its longitude is a1,a2.
I would use pdist calculate many points,but i not very well understood it. It's someone help me? Thanks a lot!

Answers (1)

R = 6371000 %meters
distfcn = @(p1, p1) R * acos(cos(p1(1))*cos(p2(1))*cos(p1(2)-p2(2)) + sin(p1(1))*sin(p2(1)));
latlong = [lagitudes(:), longitudes(:)];
distances = pdist(latlong, distfcn);

1 Comment

However,it's difference from this
d=rand(1000,2)*100;d1=zeros(1000,1000);R=6378;
distfcn = @(XI,XJ)(R*acos(cos(XI(1))*cos(XJ(1))*cos(XI(2)-XJ(2))+sin(XI(1))*sin(XJ(1))));
D1=pdist(d,distfcn);
D=squareform(D1);
for i=1:1000
for j=i:1000
d1(i,j)=R*acos(cos(d(i,1))*cos(d(j,1))*cos(d(i,2)-d(j,2))+sin(d(i,1))*sin(d(j,1)));
end
end
d1=triu(d1)'+triu(d1);

Sign in to comment.

Asked:

on 8 May 2018

Edited:

on 9 May 2018

Community Treasure Hunt

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

Start Hunting!