Calculate the distance between points in a large data set

29 views (last 30 days)
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)

Accepted Answer

John D'Errico
John D'Errico on 26 Apr 2022
Edited: John D'Errico on 26 Apr 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)
xy = 10×2
0.2803 0.3193 0.9641 0.8875 0.5832 0.6368 0.6403 0.7578 0.2228 0.1365 0.0092 0.1882 0.9569 0.2397 0.2748 0.5617 0.0680 0.6066 0.3649 0.6260
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)
ans = 9×2
0.6838 0.5682 -0.3809 -0.2507 0.0571 0.1210 -0.4175 -0.6213 -0.2135 0.0517 0.9476 0.0515 -0.6820 0.3220 -0.2069 0.0449 0.2970 0.0194
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
ans = 9×2
0.4676 0.3228 0.1451 0.0628 0.0033 0.0146 0.1743 0.3861 0.0456 0.0027 0.8980 0.0027 0.4652 0.1037 0.0428 0.0020 0.0882 0.0004
And then we need to add the corresponding squares, like this:
sum(diff(xy).^2,2)
ans = 9×1
0.7904 0.2079 0.0179 0.5604 0.0483 0.9007 0.5689 0.0448 0.0886
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))
sequentialDistances = 9×1
0.8891 0.4560 0.1338 0.7486 0.2197 0.9490 0.7542 0.2117 0.2976
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.

More Answers (2)

Jan
Jan on 26 Apr 2022
Do not create a pile of variables but a vector, which contains the distances:
Distance = sqrt(diff(x).^2 + diff(y).^2);

Steven Lord
Steven Lord on 26 Apr 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)
results = 9×3 table
johnApproach vecnormApproach hypotApproach _________________ _________________ _________________ 0.679646068050395 0.679646068050395 0.679646068050395 0.309985055717871 0.309985055717871 0.309985055717871 0.715242601010436 0.715242601010436 0.715242601010436 0.757131891929811 0.757131891929811 0.757131891929811 0.829131174065791 0.829131174065791 0.829131174065791 0.349734417873315 0.349734417873315 0.349734417873315 0.288760252713818 0.288760252713818 0.288760252713818 0.500658367483089 0.500658367483089 0.500658367483089 0.310132712376369 0.310132712376369 0.310132712376369
Those results seem to match to me.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!