To find the distance between points in an array in Matlab, you can use the pdist
function. This function calculates the pairwise distances between the observations in an array. For example, if you have an array points
with n
observations and m
dimensions, you can calculate the pairwise distances between the observations using the following code:
main.m19 chars2 lines
By default, pdist
calculates the Euclidean distance between points, but you can also specify other types of distances, such as Manhattan or Chebyshev, using the distance
parameter. For example, to calculate the Manhattan distance between points, you can use:
main.m32 chars2 lines
Note that the output of pdist
is a condensed distance matrix, which is a vector containing the distances between each pair of observations. To convert this vector to a square distance matrix, you can use the squareform
function:
main.m19 chars2 lines
This will give you a n
x n
matrix D
where D(i,j)
is the distance between observation i
and observation j
.
gistlibby LogSnag