Code covered by the BSD License  

Highlights from
Distance Matrix

4.6

4.6 | 5 ratings Rate this file 26 Downloads (last 30 days) File Size: 2.4 KB File ID: #15145
image thumbnail

Distance Matrix

by

 

29 May 2007 (Updated )

fast, vectorized distance matrix calculation

| Watch this File

File Information
Description

Returns the point-to-point distance between all pairs of points (similar to PDIST in the Statistics Toolbox, for those without it)
 
DMAT = DISTMAT(XY) Calculates the distance matrix using an automatic option
DMAT = DISTMAT(XY,OPT) Uses the specified option to compute the distance matrix
[DMAT,OPT] = DISTMAT(XY) Also returns the automatic option used by the function
 
Inputs:
XY is an NxP matrix of coordinates for N points in P dimensions
OPT (optional) is an integer between 1 and 4 representing the chosen method for computing the distance matrix (see note below)
 
Outputs:
DMAT is an NxN matrix, where the value of DMAT(i,j) corresponds to the distance from XY(i,:) to XY(j,:)
OPT (optional) is an integer between 1 and 4 representing the method used to compute the distance matrix (see note below)
 
Note:
DISTMAT contains 4 methods for computing the distance matrix
 OPT=1 Usually fastest for small inputs. Takes advantage of the symmetric property of distance matrices to perform half as many calculations
 OPT=2 Usually fastest for medium inputs. Uses a fully vectorized method
 OPT=3 Usually fastest for large inputs. Uses a partially vectorized method with relatively small memory requirement
 OPT=4 Another compact calculation, but usually slower than the others

Acknowledgements

This file inspired 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 (7)
24 Apr 2014 Andres Tovar  
19 Mar 2014 Usman Ali

Newbie: can I use this function to compute distance between two matrices.
e.g [A]= 9x6 and [B]= 1x6. and I want to the distance matrix between A and B.

19 Jul 2012 WurmD

(in the code, the automatic option selection)
numels = n*n*dims;
opt = 2; if numels > 5e4, opt = 3; elseif n < 20, opt = 1; end

The choice of opt 3 if "n*n*dims > 5e4" might be too conservative.
Those with 8 GB of RAM might want to change it to "n*n*dims > 2e8" safely (tested with the code below)

- One thing I would like help understanding is, why is the double for loop faster than and of the distmat options (in all tested situations)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rabo = rand(600);

tic,
distrabo = zeros(size(rabo,1));
for i = 1:size(rabo,1)
for j = i:size(rabo,1)
distrabo(i,j) = norm(rabo(i,:)-rabo(j,:));
distrabo(j,i) = distrabo(i,j);
end
end
toc,

%%
tic,
[distraboCompare opt] = distmat(rabo,1);
fprintf('Opt1 supposedly faster for smaller inputs '), toc,
%%
tic,
[distraboCompare opt] = distmat(rabo,2);
fprintf('Opt2 supposedly faster for medium inputs '), toc,
%%
tic,
[distraboCompare opt] = distmat(rabo,3);
fprintf('Opt3 only half vectorized for less memory '), toc,
%%
tic,
[distraboCompare opt] = distmat(rabo,4);
fprintf('Opt4 fully vectorized but for also less memory '), toc,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

17 Nov 2010 Peter Nave  
29 Jun 2007 Nina Hendrarini  
06 Jun 2007 Jon Dattorro

For more methods of fast computation of distance matrices, see the book:
Convex Optimization & Euclidean Distance Geometry, Dattorro
http://convexoptimization.com

05 Jun 2007 Siyi Deng

Good implementation. Nice examples and screen shots.

Contact us