| MatArray Toolbox | Search  Help Desk |
| dl2c | Examples See Also |
dl2c is a mex-file that calculates the
euclidian distance between the columns
of a matrix or between the columns of two matrices.
Syntax
d = dl2c(a) d = dl2c(a,[]) d = dl2c(a,b)
Description
dl2c is a fast mex-file implementation of
the euclidian distances between columns. It is useful for the quick
calculation of distances in K-means or hierarchical clustering.
The gain in time compared to an m-file implementation varies, but
is usually at least 4, and over 20 compared to pdist.
The result is a simple sum of
squares, it is not divided by the number of rows and the square
root is not taken.
The first version of the call returns a symmetrical
square matrix d whose values are the squared distances
between the columns
of the matrix a (e.g. d(2,5) is the
squared distance between the second and the fifth column of
the matrix a, that is
sum( (a(:,2)-a(:,5)).^2 )).
The second version returns a linear matrix of distances, in
a fashion similar to pdist in the statistic toolbox.
The advantage is that no redundant information is stored, hence
a gain in space of a factor of about 2. It is possible to obtain
the square form with a call to squareform. See the
statistic toolbox for more information.
The third version returns the distances between the columns
of the matrices a and b. The element
d(i,j) is the distance between the vectors
a(:,j) and b(:,i)
Examples
X = [1 2; 1 0; 2 2]
X =
1 2
1 0
2 2
dl2c(X)
ans =
0 2
2 0
dl2c(X')
ans =
0 4 1
4 0 5
1 5 0
dl2c(X',[])
ans =
4
1
5
Y = [0 3; 2 1; 4 1]
Y =
0 3
2 1
4 1
dl2c(X,Y)
ans =
6 12
5 3
See Also
dl2cnan,
pdist,
squareform