|
|
| File Information |
| Description |
function D = sqdistance(A, B, M)
% Square Euclidean or Mahalanobis distances between all sample pairs
% A: d x n1 data matrix
% B: d x n2 data matrix
% M: d x d Mahalanobis matrix
% D: n1 x n2 pairwise square distance matrix
% d and n are the dimension and the number of vectors.
The output is a matrix of square of Euclidean or Mahalanobis distances between vectors in the inputs. Column vectors are assumed in this code.
When two matrices A and B are inputed, this function computes the square Euclidean distances between them. If an extra positive definite matrix M is inputed, it computes Mahalanobis distances.
If only one matrix A is inputed, it computes square Euclidean distances between vectors in A. In this case, it is equivalent to the square of pdist function in matlab statistics toolbox but faster.
Sample code:
d=1000;n1=5000;n2=6000;
A=rand(d,n1);B=rand(d,n2);
M=rand(d,d);M=M*M'+eye(d);
D1=sqdistance(A,B);
D2=sqdistance(A);
D3=sqdistance(A,B,M); |
| MATLAB release |
MATLAB 7.8 (R2009a)
|
| Zip File Content |
|
| Other Files |
license.txt, sqdistance.m
|
|
Tags for This File
|
| Everyone's Tags |
|
| Tags I've Applied |
|
| Add New Tags |
Please login to tag files.
|
| Updates |
| 01 Jul 2009 |
update the description |
| 02 Jul 2009 |
Add a centerization step for robustness purpose. Split the code for different number of input for efficiency purpose. Update comments. |
| 02 Jul 2009 |
update to support Mahalanobis distance. fix a bug for one dimensional case. |
| 03 Jul 2009 |
remove any redundant error check |
|
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