Clear Filters
Clear Filters

Help me to find nearest latitude and longitude between a bunch of latitudes and longitudes

12 views (last 30 days)
Hello, I have a problem that I don't know how to fix it. It doesn't complicated problem but I can't solve it.
I have some station names and corresponding latitude, longitude and elevation. I want to find the nearest stations. In fact for each row, I need to compute the distance to all other rows and pick the one with the smallest distance. I want to save some computation by realizing that the distances are symmetric, and so need to compute only n*(n-1)/2 distances. I want to have the name of the nearest station together in a table to read them.
I attached coordinates2.mat
Here what I've done so far:
load coordinates2.mat
%start with one station:
Ahvaz = coordinates{5,{'lat','lon','station_elevation'}}; %Ahvaz is the 5th station in the list
coordinates(5,:)=[] %eliminate Ahvaz form list in order to pervent minimum distance going to be 0
All = coordinates{:,{'lat','lon','station_elevation'}};
%%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, All, Ahvaz).^2,2));
%find the smallest distance and use that as an index into All:
closest = All(distances==min(distances),:);
%result show me the lat,lon,and elevation of nearest station to Ahvaz,
% After that I should check list to find the name of it
%then should I clear and load coordinates2.mat in order to have Ahvaz again
% and check other stations
The main problem is this code isn't automatically changed over all stations. I should change it manually. also when the output (closest) achieved I should look at the list of the coordinate and finde match corresponding number by my eyes. I want to print the nearest stations in a table in order to analyze them in a more efficient way.
Thank you all.

Accepted Answer

Mohammad Sami
Mohammad Sami on 26 Jan 2020
You can do it like this
dist = sqrt((coordinates.lon - coordinates.lon').^2 + (coordinates.lat - coordinates.lat').^2);
dist(dist == 0) = Inf;
[~,closest_id] = min(dist);
coordinates.closest_station = coordinates.station_name(closest_id);

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!