Calculate the distance between points in a large data set
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel Gaggini
el 26 de Abr. de 2022
Respondida: Steven Lord
el 26 de Abr. de 2022
I have a number of x and y coordinates.
I want to find the distance (sqrt(x^2+y^2)) from a point to the point in behind it, like in the image below.
What would be the best way to do this without using this code forever.
Distance_x_1 = (x(2,1) - x(1,1))^2
Distance_y_1 = (y(2,1) - y(1,1))^2
Distnace_1 = sqrt(Distance_x_1+Distance_y_1)
Distance_x_2 = (x(3,1) - x(2,1))^2
Distance_y_2 = (y(3,1) - y(2,1))^2
Distnace_2 = sqrt(Distance_x_1+Distance_y_1)
0 comentarios
Respuesta aceptada
John D'Errico
el 26 de Abr. de 2022
Editada: John D'Errico
el 26 de Abr. de 2022
LEARN TO USE MATLAB. Do not create numbered and named variables. DO create vectors and arrays. Use them.
Since all you have posted is a picture of your data, I cannot use it.
xy = rand(10,2)
Now you seem to be asking how to compute the sequence of distances from each point in the list to the point immediately behind it in the sequence.
That means we must subtract x(i-1) from x(i), and y(i-1) from y(i). Is there any function that will do that directly? YES. If you don't know it, then you need to be reading the tutorials. So what does diff do here?
diff(xy)
Do you see that diff does exactly what we are looking for? Now, we need to square those differences, forming (x(i) - x(i-1))^2, and the same for y.
diff(xy).^2
And then we need to add the corresponding squares, like this:
sum(diff(xy).^2,2)
That is getting closer. Now we just need a square root, and then assign the result into a new variable to be used later.
sequentialDistances = sqrt(sum(diff(xy).^2,2))
When you cannot figure out how to solve a problem that is too complicated for you to solve, look for tools that will do at least part of what you need, even if only a small part. Then look for ways to do the rest of the task, getting you closer all the time to your target. Finally, put it all together.
Most importantly, learn to use arrays, to use vectors.
0 comentarios
Más respuestas (2)
Jan
el 26 de Abr. de 2022
Do not create a pile of variables but a vector, which contains the distances:
Distance = sqrt(diff(x).^2 + diff(y).^2);
0 comentarios
Steven Lord
el 26 de Abr. de 2022
John showed you one approach. Two others that come to mind are to use the hypot or vecnorm functions.
% Sample data
xy = rand(10,2);
johnApproach = sqrt(sum(diff(xy).^2,2));
% Take the 2-norm (the first 2 below) along dimension 2
vecnormApproach = vecnorm(diff(xy, 1), 2, 2);
d = diff(xy, 1);
hypotApproach = hypot(d(:, 1), d(:, 2));
% Show the results
format longg
results = table(johnApproach, vecnormApproach, hypotApproach)
Those results seem to match to me.
0 comentarios
Ver también
Categorías
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!