Problem 43007. Euclidean interpoint distance matrix
The Euclidean distance between two points in a pdimensional space is a really common thing to compute in the field of computational geometry. In fact, it is pretty easy to do. norm(uv) would seem to do the trick, for vectors u and v.
But what if you have a large number of points between which you want to compute ALL of those distances between every pair of points? In this problem, given an n by p array A, where each row of the matrix will be viewed as containing the coordinates of one pdimensional point, you need to compute the n by n interpoint distance matrix.
Thus, D(i,J) will be the Euclidean distance between the i'th and j'th rows of A. Of course, D will be a symmetric matrix, with zeros on the diagonal.
So you can test your code, here is an example:
A = [1 1 5 2 2 2 4 5]
format short g D = interDist(A) D = 0 4.1231 1.4142 5 4.1231 0 3 3.1623 1.4142 3 0 3.6056 5 3.1623 3.6056 0
Thus, thinking of the points [1 1] and [5 2] as the coordinates of two points in the (x,y) plane, the Euclidean distance between them in that plane is 4.1231...
As you can see, the matrix is symmetric, with zeros on the main diagonal. That must always happen, since the distance between any point and itself must be zero.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers25
Suggested Problems

Find all elements less than 0 or greater than 10 and replace them with NaN
15472 Solvers

2229 Solvers

233 Solvers

Getting the indices from a matrix
633 Solvers

Given a matrix, swap the 2nd & 3rd columns
1039 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!