Compute the minimum distance between each element in a vector with another larger array
조회 수: 9 (최근 30일)
이전 댓글 표시
I have an array say, MW that contains the latitudes and longitudes of ~1300 pixels. I also have a reference text file, say IR that contains an enormously large number of pixels with latitudes and longitudes and the pixels here are associated with unique IDs. My goal is to attribute each MW pixel to a unique ID found in the IR text file if possible. I have a function called distance_latlon (function d = distance_latlon(lat1,lat2,lon1,lon2)) that computes the distances between any two points given their lat and lon. Essentially, I would like to compute for each MW pixel, the distance to all of the IR pixels and find the IR pixel within the closest distance and attribute the ID. If the MW pixel does not have a single IR pixel within a certain distance threshold, then I can simply assign a 0.
How do I accomplish this task without the extremely expensive way that is, going through each MW pixel in a for loop, running the distance function between each MW pixel and all the IR pixels in the text file in another long for loop and finding the minimum distance? What is the least expensive way of doing this?
댓글 수: 2
Bruno Luong
2020년 8월 19일
편집: Bruno Luong
2020년 8월 19일
What is the size of IR (something more precise than "enormously large number")? If you provide the size of MW, it would be nice if you provide the size of IR as well.
What returns distance_latlon(lat1,lat2,lon1,lon2)? Is it the geodesic distance on sphere?
채택된 답변
Rik
2020년 8월 19일
I don't think there is a way around doing some calculation for each pair.
Is there a way to vectorize distance_latlon? If not, I would suggest a pre-selection, so you only need to do the expensive calculation a few times:
N_small=5;
[lat2,lon2]=MyFun(IR);
for n_MW=1:1300
[lat1,lon1]=MyFun(MW(n_MW));
quickdist=hypot(lat1-lat2,lon1-lon2);
[~,idx1]=sort(quickdist);
lat2_small=lat2(idx(1:N_small));
lon2_small=lon2(idx(1:N_small));
for n_small=1:N_small
exactdist(n_small)=distance_latlon(lat1,lat2_small,lon1,lon1_small);
end
[min_exactdist,idx2]=min(exactdist);
rowID=idx1(idx2);
end
댓글 수: 6
Rik
2020년 8월 21일
Sure, the part with quickdist was based on the assumption that running it directly was too slow.
That function does seem to support vector input, so I would suggest you try it to see if it suits your need.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Mapping Toolbox에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!