View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Computing Pairwise Distances and Metrics

4.5 | 4 ratings Rate this file 18 Downloads (last 30 days) File Size: 18.2 KB File ID: #15935 Version: 1.0

Computing Pairwise Distances and Metrics


Dahua Lin (view profile)


16 Aug 2007 (Updated )

Compute pairwise distance or metric values between two sets of vectors

| Watch this File

File Information

slmetric_pw.h is an m-function to compute metrics between two sets of vectors in pairwise way.

Main Features:

-- It supports about 20 metric types, including Euclidean distance (L2), Normalized Correlation, City-Block distance (L1), Quadratic distance, Minkowski distance, Hamming distance, Histogram Intersect, Chi-square distance, and information theoretical divergences, etc.
-- It is highly optimized by taking full advantage of vectorized computation. For some distances that are difficult to be fully vectorized, like city-block distance, C-mex implementation is offered.
-- It is easy to use with simple syntax.
-- It is sufficiently documented. You can type in "help slmetric_pw" to see the help on its usage.
Examples are available to show how to invoke the function.


slmetric_pw.h is an important function in the core module of sltoolbox, which is a large set of functions for accomplishing many statistical learning tasks.
I am now developing the second version of sltoolbox. Considering that some of the core functions can be applied to a much wider domain in technical computing, and that a long time is needed to make a new version of the whole toolbox ready, I would like to release some of these functions independently so that more fields can benefit from them.
The function has been substantially rewritten based on new features of MATLAB 2007a with enhanced efficiency and stability.


% prepare sample matrix
X1 = rand(10, 100);
X2 = rand(10, 150);

% compute the euclidean distances (L2) between the samples in X1 and X2
M = slmetric_pw(X1, X2, 'eucdist');

% compute the eucidean distances between the samples in X1 in a pairwise manner
M = slmetric_pw(X1, X1, 'eucdist');

% compute the city block distances (L1)
M = slmetric_pw(X1, X2, 'cityblk');

% compute the normalize correlations
M = slmetric_pw(X1, X2, 'nrmcorr');

% compute hamming distances
M = slmetric_pw(X1, X2, 'hamming', 0.5);
M2 = slmetric_pw((X1 > 0.5), (X2 > 0.5), 'hamming');
assert(isequal(M, M2));

% compute weighted squared distances with user-supplied weights
weights = rand(10, 1);
M = slmetric_pw(X1, X2, 'wsqdist', weights);

% compute quadratic distances (x-y)^T * Q (x-y)
Q = rand(10, 10);
M = slmetric_pw(X1, X2, 'quaddiff', Q);

% compute Minkowski distance of order 3
M = slmetric_pw(X1, X2, 'minkowski', 3);


This file inspired K Nearest Neighbor and Ipdm: Inter Point Distance Matrix.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
19 Mar 2016 Pooi Hieang Loo

Hi, I just noticed that I couldn't run the Hamming distance. I hope you could help me.

let A=14X2, B=14X2, t=[0]

Undefined function or variable 'pwhamming_cimp".

Error in slmetric_pw (line289)

May I know how should i use this 'hamming distance' function?

Comment only
29 Feb 2012 Sahar

Sahar (view profile)

I noticed that the intersect function in the c++ implementation computes sum(min(u1,u2))/max(sum(u1),sum(u2)) while in the description/help of the slmetric_pw.m it is written that it computes sum(min(u1,u2))/min(sum(u1),sum(u2)).


Comment only
16 Aug 2010 Segun Oshin


I noticed that it has mexw32 files. Does that mean it can only be compiled and used on Windows? Would it work on Linux?


Comment only
11 Dec 2009 D

D (view profile)

Please ignore my previous post - I must have been sleeping when submitting this.

11 Dec 2009 D

D (view profile)

This function does not seem to produce symmetrical distance matrices when using 2 sets of data. For example:

A = rand([50 100]);
B = rand([50 100]);
M = slmetric_pw(A,B, 'eucdist');
[M(66,10);M(10,66)]%NOT symmetrical
M = slmetric_pw(A,A,'eucdist');

Comment only
01 Dec 2009 Bob Smith

Fast and versatile, I use this for computing kernels

18 Jul 2009 Amandeep Bharti

09 Oct 2008 Dahua Lin

To Amir AM: it accepts matrices with each column regarded as a sample. For other types of input, you may use some array manipulation functions like reshape or permute to make it in this form.

Comment only
14 Sep 2008 Amir AM.

Thank you Lin.Are the input arguments must be column vectors or we can use matrix ?

11 Oct 2007 Amir Blind

Thanks a lot.

Comment only

Contact us