Any faster way to calculate sphere distance between two tables of points?
1 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I have been stuck recently. I'm trying to calculate distance between every point in table A and B, based on their longitudes and latitudes. However, my current solution is quite time-consuming and I think there must be a better way to solve it. With my limited Matlab knowledge, I haven't figured out a new implementation. The tables are shown as below, TableA (103376x6) and TableB (32x3):
I want to get the distance of each point of tableA to all the points in tableB. So the resulting matrix, dist, is a 103376 x 32 matrix. What I have done was to use two for loops, which is so inefficient with huge amount of data. So, how can I do to make it run faster? Appreciate your suggestions and comments.
for i=1:height(A)
for j=1:height(B)
dist(i,j) = distance(A.Lat(i),A.Lon(i),B.Lat(j),B.Lon(j))/180*pi*6378.137;
end
end
4 个评论
Jess Lovering
2019-8-15
Can you make 4 matrices that are each 103376 x 32 matrix (one for each of the lat1, lon1, lat2, lon2) and perform the distance calculation once? You could use the repmat command to replicate the lon in TableA 32 times and then repeat that for the lat. Then transpose the lat and lon arrays in TableB to be one row and 32 columns then repmat that 103376 times. Then you would have 4 matrices that are all 103376 x 32 that you could input to the distance command. Not sure what the time difference would be but I imagine it would be much faster.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!