Compute 3D distance between 32 points

I have encountered the following problem: I have to divide a square with L=12 in 4x4 smallers squares and find the center point of each small square. This will represent surface 1 and then i have to do the exact thing for surface 2.
Now, i have to compute the distances between each center of surface 1 and each center of the surface 2. So i will have a total of 256 distances. How do i do that ? Check out the photos.

 Risposta accettata

To find the distance from every one of 16 points to every one of another set of 16 points would give 16 * 16 = 256 distances. You can get these out in a 16 by 16 2-D array using pdist2(). Attach your data (2 lists of points) in a .mat file if you need more help.
distances = pdist2(xySet1, xySet2);

Più risposte (1)

The distance beween two points, p1 and p2, in 3d space is the square root of (x2 - x1)^2 + (y2-y1)^2 + (z2-z1)^2.
So let's have 2 matrices representing the centerpoints in surface 1 and two:
s1 = [p1x p1y p1z
p2x p2y p2z
p3x p3y p3z];
s2 = [p1x p1y p1z
p2x p2y p2z
p3x p3y p3z];
Where each column represents the x,y, and z components of a point. Then just sum the squares of the differences and take the square root
d= s2-s1;
sq = d.^2;
distance = sqrt(sum(sq,2)) % sum up along the row elements
You should be returned with a vector containing the distances between each pair of points from the two surfaces

2 Commenti

Or, if you have Statistics and Machine Learning Toolbox, pdist2 function will be some help.
Thank you for answering !
I tried your method but the result will be a vector of 4 elements and the total number of elements shoud be 256 because on surface 1 i have 16 center points and on surface 2 i have other 16 center points. I have to compute the distance between each center point. So for center point 1 surface 1 to center point 1:n of surface 2.
It will be 16 distances between center point 1 of surface 1 and all the center points of surface 2. Then there are 15 other center points for surface 1.
I hope i explained well.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by