pdist2

Compute distances between rows of two matrices.

Syntax

d = pdist2(m1, m2)

d = pdist2(m1, m2, metric)

Inputs

m1
The first matrix on which to operate.
Type: double
Dimension: matrix
m2
The second matrix on which to operate, having the same number of columns as m1.
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.
'chisq'
Chi-squared distance.
'cosine'
One minus the included angle between two vectors.
'emd'
Earth Mover's Distance.
'L1'
The L1 distance, or the sum of absolute coordinate differences.
Type: string

Outputs

d
The distance values. If m1 and m2 have p and q rows, respectively, then d has dimensions pxq.
Dimension: matrix

Example

m1 = [1,2,3;4,6,8;5,10,15;16,26,36];
m2 = [2,4,6;3,6,9;4,8,12];
p = pdist2(m1, m2)
p = [Matrix] 4 x 3
 3.74166   7.48331  11.22497
 3.46410   1.41421   4.47214
11.22497   7.48331   3.74166
39.74921  36.02777  32.31099

Comments

Element d(i,j) is computed from m1(i,:) and m2(:,j).