Borrar filtros
Borrar filtros

How to calculate the minimum distance between two points?

11 visualizaciones (últimos 30 días)
Ivan Mich
Ivan Mich el 22 de Sept. de 2022
Respondida: the cyclist el 22 de Sept. de 2022
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,
filename1= 'file1.txt';
[d1,tex]= importdata(filename1);,2);,1);
for z=1:size(lat1,1);
for j=1:size(t,1);
but I realise that it is two "slow" and time consuming.
Is there any way to make this code more "fast"??
  2 comentarios
the cyclist
the cyclist el 22 de Sept. de 2022
Can you upload the data files? (Use the paper clip icon from the INSERT section of the toolbar.)
Ivan Mich
Ivan Mich el 22 de Sept. de 2022
I am uploading these files. I hope that you can help me...
Thank you

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 22 de Sept. de 2022
I am not absolutely certain that this code is equivalent to what you wrote, because I cannot test it on my local machine. (I do not have a toolbox with the distance function.)
However, I believe that you can vectorize one of the inputs to that distance function, and you should get a large speedup. (The non-vectorized version did not even finish within the timeout period here.)
% Start timer
% Read data from online files. (You should read from your local files instead.)
filename1= '';
filename2= '';
% Read in the data. (Used readtable instead of importdata.)
d1 = readtable(filename1);
d2 = readtable(filename2);
% Extract the latitude and longitude variables from the two files.
% (Uses curly brackets, to get the column contents, rather than a single-column table.)
lat1 = d1{:,2};
lon1 = d1{:,1};
lat2 = d2{:,2};
lon2 = d2{:,1};
% Preallocate the output, for better memory management
output = zeros(size(d1,1),size(d2,1));
% Calculate the distances
for i1 = 1:size(lat1,1)
Elapsed time is 2.003897 seconds.
Note that I made a couple other changes. The first is that I used readtable instead of importdata, so that it would work here. Second, I changed several variable names so that these names were treated consistently between your two files.
I suggest you thoroughly debug what I changed (perhaps on a small subset of your data), to make sure you are still getting the result you expect.
I hope that helps!

Más respuestas (1)

the cyclist
the cyclist el 22 de Sept. de 2022
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.


Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by