How to calculate the minimum distance between two points?
古いコメントを表示
Helo,
I have a question about a code. I have two input files (file1.txt and file2.txt), each of them have three columns: the 1st has the latitude, the 2nd the longtitude and the 3rd one numerical value. The second .txt file has 200000 lines and the first .txt file has 1000 lines. I would like to calculate the minimum distance for each point of file 1 from the file2.txt (I mean that I would like to find the closest point of file2.txt for each point of file1.txt).
I am using the following lines,
clc
clear
filename1= 'file1.txt';
[d1,tex]= importdata(filename1);
lat1=d1.data(:,2);
lon2=d1.data(:,1);
t=importdata('file2.txt');
lat2=t(:,2);
lon2=t(:,1);
z2=t(:,3);
for z=1:size(lat1,1);
for j=1:size(t,1);
output(z,j)=(deg2km(distance(lat1(z),lon1(z),lat2(j),lon2(j))));
end
end
V=output';
%==================DISTANCE================================
[M,I]=min(V,[],2);
but I realise that it is two "slow" and time consuming.
Is there any way to make this code more "fast"??
2 件のコメント
the cyclist
2022 年 9 月 22 日
Can you upload the data files? (Use the paper clip icon from the INSERT section of the toolbar.)
Ivan Mich
2022 年 9 月 22 日
採用された回答
その他の回答 (1 件)
the cyclist
2022 年 9 月 22 日
I doubt that this is the main issue slowing down the code, but you should preallocate the memory for your output array. Growing an array incrementally can lead to very inefficient memory usage. See more detail here.
Put the line
output = zeros(size(lat1,1),size(t,1));
before your for loops.
カテゴリ
ヘルプ センター および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!