pdist

Compute distances between the rows of a matrix.

Syntax

d = pdist(m)

d = pdist(m, metric)

d = pdist(m, 'minkowski', p)

Inputs

m
The matrix on which to operate.
Type: double
Dimension: matrix
metric
The distance function to compute.
The available options are as follows:
'euclidian' (default)
Euclidean distance.
'sqeuclidian'
Square of the Euclidean distance.
'seuclidian'
Standardized Euclidean distance, the Euclidean distance normalized by the sample variance.
'mahalanobis'
Mahalanobis distance.
'cityblock'
City Block distance.
'minkowski'
Minkowski distance, also known as the p-norm.
'cosine'
One minus the included angle between two vectors.
'correlation'
One minus sample correlation between two vectors.
'spearman'
One minus sample Spearman's rank correlation between two vectors.
'hamming'
Hamming distance.
'jaccard'
One minus the Jaccard distance.
'chebychev'
Chebychev distance, the maximum coordinate difference.
Type: string
p
The parameter in the Minkowski distance (default: 2).
Type: integer
Dimension: scalar

Outputs

d
The distance values.
Dimension: vector

Example

m = [1,2,3;4,6,8;5,10,15;16,26,36];
d = pdist(m, 'cityblock')
p = [Matrix] 1 x 6
12  24  72  12  60  48

Comments

If m contains r rows then d is a row vector with r*(r-1)/2 elements. The first r-1 elements are the distances between the first row and the remaining r-1 rows. The next r-2 elements are the distances between the second row and the remaining r-2 rows, and so on.