You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to find the nearest point for 71 points?
2 views (last 30 days)
Show older comments
Hey all,
I have coordinates.mat which includes the name of 71 climate stations and corresponding latitude and longitude. I want to know the nearest stations. I want something like this:
Station lat lon NEAR_STATION *(NEW COLUMN IN FRONT OF EACH ROW)*
------------------------------------------------------------------------------------------
Ahvaz 20.32 33.3 Abadan
Abadan 20.30 33 Ahvaz
Tehran 36.72 52.5 Qum
I want to find the nearest stations for all these 71 stations. once I know what is the name of the nearest station I can go ahead and using linear regression to reconstruct missing data.
thank you in advance
I attached coordinates.mat
14 Comments
Walter Roberson
on 25 Jan 2020
Which distance measure are you using? Is Euclidean distance good enough for your purposes? If not, then is Sphere an acceptable approximation https://www.mathworks.com/matlabcentral/fileexchange/23026-compute-the-great-circle-distance-between-two-points or do you need a reference ellipsoid? https://www.mathworks.com/help/map/ref/distance.html ?
You can use pdist() . If you need something other than Euclidean distance then you can pass a handle to the distance function like
pdist(Points, @(Z1,Z2) distance(Z1(1), Z1(2), Z2(1), Z2(2)))
It is common to want to use squareform() on the output of pdist()
BN
on 25 Jan 2020
Is there any chance for dsearchn? I'm new to this topic. I have 71 points (latitude and longitude creates a point actually) I want to find the nearest points for all these 71's. So I want to find one nearest point for every 71 points that I have. I think it would be great if the distance between them can be calculated by kilometer. I don't have any idea to do this. any help is appreciated.
Thank you and Best Regards
Walter Roberson
on 25 Jan 2020
dsearchn always uses Euclidean distance. That might or might not be acceptable to you. depending on the span of latitudes and the distance from the equator. If map distance is needed then use https://www.mathworks.com/help/stats/knnsearch.html knnsearch with the 'Distance' option naming a function handle such as the distance() function from the Mapping Toolbox
I think it would be great if the distance between them can be calculated by kilometer
With the span of latitudes that you have, you cannot get kilometer accuracy using Euclidean distance.
>> 69*1.6093*cosd(20.3)
ans =
104.144781710494
>> 69*1.6093*cosd(36.72)
ans =
89.0073606639364
so you have a difference of about 15 kilometer per degree of longitude because of latitude effects.
Walter Roberson
on 26 Jan 2020
I notice that your example coordinates are pretty far off from reality. It might be the case that your actual coordinates are all close enough together that you can use Euclidean distance. However, at your lowest latitude, 20.30, an additional 1.44 degrees north makes a difference of 1 kilometer per degree of longitude due to spherical correction, so your stations would have to be really close together or closer yet to the equator to get kilometer accuracy using Euclidean distance.
BN
on 26 Jan 2020
Edited: BN
on 26 Jan 2020
Yes, you are right. all the latitude and longitude stands for climate stations in a country. The biggest problem that is I have 2 things (latitude and longitude) and I want to search along 2 columns (latitude and longitude). I could handle it if they are just one point using this code:
Ahvaz = coordinates(5,{'lat','lon'})
All = coordinates(:,{'lat','lon'})
%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(find(distances==min(distances)),:);
If I knew how to automatically finde the nearest station for Ahvaz (in station_name column) and prints the name of it. I could repeat the process for all stations and find my answer. I want to use the Euclidean distance.
Mohammad Sami
on 26 Jan 2020
Edited: Mohammad Sami
on 26 Jan 2020
You can try to use interp2, it will help you interpolate in 2 dimension.
Vq = interp2(X,Y,V,Xq,Yq,'nearest'); % use the method nearest
X = lat, Y = lon, V = StationID, Xq = lat_of_missing, Yq = lon_of_missing.
Do note as Walter mentioned, that this is will not use Euclidean distance.
BN
on 26 Jan 2020
For each point, I need to compute the distance to all other points, 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.
Walter Roberson
on 26 Jan 2020
I would tend to suspect you should be using
Ahvaz = coordinates{5,{'lat','lon'}};
All = coordinates{:,{'lat','lon'}};
Caution:
closest = All(find(distances==min(distances)),:);
Your All data includes Ahvaz so the minimum distance is going to be 0, which is not going to be of much use to you.
BN
on 26 Jan 2020
I moved my question to this link and modify it: https://www.mathworks.com/matlabcentral/answers/501995-help-me-to-find-nearest-latitude-and-longitude-between-a-bunch-of-latitudes-and-longitudes
thank you all
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)