Finding nearest tables based on latitude and longitude columns
1 回表示 (過去 30 日間)
古いコメントを表示
Hey all
I have two cells; cell A and cell B. Cell A is 11 x 1, and cell B is 200 x 1. These cells include tables.
Each table in both cells has a column named lat (latitude) and lon (longitude). Latitude and longitude are constant in each table but different in A and B.
I mean, I have some points (longitude and latitude) in cell A and some other points with different latitude and longitude in cell B.
I want to find which table in cell B is the nearest to the tables in cell A, based on latitude and longitude columns (Euclidean distance).
I want to store every two tables that are nearest to each other.
So, in the end, for every 11 tables in A I would have 11 other tables from B that are nearest to A tables.
% I know it maybe something like this but I don't know rest of it%Finding nearest station one-by-one
dist = sqrt((A.lon - A.lon').^2 + (B.lat - B.lat').^2);
dist(dist == 0) = Inf;
[~,closest_id] = min(dist);
I attached A and B here.
Thank you all in advance.
2 件のコメント
Star Strider
2020 年 2 月 13 日
For accurate geographical distance, use great circle distances, not rhumb line distances.
採用された回答
Adam Danz
2020 年 2 月 13 日
編集済み: Adam Danz
2020 年 2 月 13 日
Inputs
- A is a 1xn or n1x cell array of tables; each table contains the headers 'lat' and 'lon' which are constants.
- B is a 1xm or mx1 cell array of tables with the same descrtiption as A.
Output
- minIdx is a nx1 vector of index values (positive integers) that identify the tabels listed in B that are closest to A (Euclidean distance). So, the table B(minIdx(i)) is closest to table A(i).
% Function that gets the [lan lon] coordinates for all elements of a cell array.
% input: c is a cell array (ie, A or B), each element is a table with variables "lat" and "lon".
% Those two columns contain constant values.
% output: nx2 matrix of [lat,lon] values.
getLatLon = @(c)cell2mat(cellfun(@(a){[unique(a.lat), unique(a.lon)]},c(:)));
% Create grid of distances between all stations.
% distGrid is nxm matrix where distGrid(i,j) is the distance
% between station i in A and station j in B.
distGrid = pdist2(getLatLon(A), getLatLon(B));
% For each station in A, find the closest station in B.
% minIdx(i) is the index of B closest to station i in A.
[~, minIdx] = min(distGrid,[],2)
You can finish it from here ;)
5 件のコメント
Adam Danz
2020 年 2 月 13 日
編集済み: Adam Danz
2020 年 2 月 13 日
I can't see it because the text is cut off but I think you're missing this
getLatLon = @(c)cell2mat(cellfun(@(a){[unique(a.lat), unique(a.lon)]},c(:)));
% Here----------------------------------------------------------------^^^^
The c(:)
Or, perhaps there's a transpose at the end which shouldn't be there (it was in my first answer but I corrected it soon after).
getLatLon = @(c)cell2mat(cellfun(@(a){[unique(a.lat), unique(a.lon)]},c(:))');
% This shouldn't be there -------------------------------------------------^
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!