# Finding unique closest point corresponding to latitude and longitude vectors for a given point with shortest distance.

20 views (last 30 days)

Show older comments

Muhammad Qaisar Fahim
on 25 Oct 2022

Commented: Bruno Luong
on 27 Oct 2022

I have two large vectors for the pair of latitudes and longitudes (Lat, Lon). I want to find a unique single pair of latitude and longitude corresponding to the point ([lat_deg, lon_deg]) which has shortest distance.

I am using this:

P = ([lat_deg, lon_deg]);

PQ = [Lat, Lon];

[k,dist] = dsearchn(P,PQ);

But at the end of this I get the distances of all the points and vector k contains all ones. Please guide if this is the right function if yes how can I correct it? if not what is the right function.

Sample vectors are:

Lat Lon

39.2591200000000 -85.9394200000000

39.2591300000000 -85.9392000000000

39.2590800000000 -85.9406300000000

39.2593500000000 -85.9406200000000

39.1949800000000 -85.9633400000000

39.1954200000000 -85.9633500000000

39.1954200000000 -85.9633500000000

39.1963300000000 -85.9633600000000

39.1957400000000 -85.9678800000000

39.1959300000000 -85.9682400000000

P=39.2005981000000 -85.9045842000000

##### 0 Comments

### Accepted Answer

KALYAN ACHARJYA
on 25 Oct 2022

#It can verify from plot too, pls modify as per requirments-

PQ=[39.2591200000000 -85.9394200000000

39.2591300000000 -85.9392000000000

39.2590800000000 -85.9406300000000

39.2593500000000 -85.9406200000000

39.1949800000000 -85.9633400000000

39.1954200000000 -85.9633500000000

39.1954200000000 -85.9633500000000]

lat=PQ(:,1);

long=PQ(:,2);

P=[39,-85];

[k,dist]=dsearchn(P,PQ);

plot(lat,long,'rx',39,-85,'bo','linewidth',2);

idx=find(min(dist)==dist);

disp('Minimum distance Point is ');

PQ(idx,:)

Hope it Helps!

##### 5 Comments

KALYAN ACHARJYA
on 27 Oct 2022

### More Answers (1)

Bruno Luong
on 27 Oct 2022

Edited: Bruno Luong
on 27 Oct 2022

Use dsearchn on lon/lat is wrong.

The right procesure is

convert lat/lon to 3D coordinates (for both list P and PQ

use dnsearch on 3D coordinates

[k] = dsearchn(P3D,PQ3D)

Then compute the geodesic distance for all points PQ to P(k) by this formula

Take the min of the list of the distances.

##### 3 Comments

Bruno Luong
on 27 Oct 2022

The wikipedia gives the formula of geodesic distance between

P1 with longitude/latitude in radian Lambda1, Phi1

P2 with longitude/latitude Lambda2, Phi2

as

d = r*dsigma

dsigma = acos(sin(Phi1)*sin(Phi2) + cos(Phi1)*cos(Phi2)*cos(Lambda1-Lambda2)

where r the radius of the earth, assumin a sphere approximation.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!