Finding nearest tables based on latitude and longitude columns

조회 수: 1 (최근 30일)
BN
BN 2020년 2월 13일
댓글: BN 2020년 2월 13일
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
Star Strider 2020년 2월 13일
For accurate geographical distance, use great circle distances, not rhumb line distances.
BN
BN 2020년 2월 13일
You are right. But I have only latitude and longitude and the elevation does not exist for my data (for A).
Thank you

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
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
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 -------------------------------------------------^
BN
BN 2020년 2월 13일
Thank you so much ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by