4.0

4.0 | 2 ratings Rate this file 165 downloads (last 30 days) File Size: 17.06 KB File ID: #15935

Computing Pairwise Distances and Metrics

by Dahua Lin

 

16 Aug 2007 (Updated 16 Aug 2007)

No BSD License  

Compute pairwise distance or metric values between two sets of vectors

Download Now | Watch this File

File Information
Description

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.

Background:

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.

Examples:

% 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);

Acknowledgements
This submission has inspired the following:
IPDM: Inter-Point Distance Matrix
MATLAB release MATLAB 7.4 (R2007a)
Zip File Content  
HTML Files pwmetric/readme.html
Other Files pwmetric/pwhamming_cimp.cpp,
pwmetric/pwhamming_cimp.mexw32,
pwmetric/pwmetrics_cimp.cpp,
pwmetric/pwmetrics_cimp.mexw32,
pwmetric/slmetric_pw.m,
pwmetric/slmetric_pw_compile.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
11 Oct 2007 Amir Blind

Thanks a lot.

14 Sep 2008 Amir AM.

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

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.

18 Jul 2009 Amandeep Bharti  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
distance Dahua Lin 22 Oct 2008 09:23:03
metric Dahua Lin 22 Oct 2008 09:23:03
pairwise Dahua Lin 22 Oct 2008 09:23:03
mathematics Dahua Lin 22 Oct 2008 09:23:03
distance Amandeep Bharti 18 Jul 2009 14:12:28
metric Amandeep Bharti 18 Jul 2009 14:12:36
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com