Problem 43007. Euclidean inter-point distance matrix
The Euclidean distance between two points in a p-dimensional space is a really common thing to compute in the field of computational geometry. In fact, it is pretty easy to do. norm(u-v) 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 p-dimensional point, you need to compute the n by n inter-point 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 Solvers27
Suggested Problems
-
4233 Solvers
-
Back to basics 17 - white space
270 Solvers
-
Sum of diagonal of a square matrix
1569 Solvers
-
Vectorizing, too easy or too hard?
142 Solvers
-
1408 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!