Home > data-import > supporting_functions > distances.m

distances

PURPOSE ^

distances calculates the distance between each pair of vectors.

SYNOPSIS ^

function distances = distances(A, B)

DESCRIPTION ^

 distances calculates the distance between each pair of vectors.

 Usage:
 distances(A, B)
   For each row in A, takes the corresponding row in B, and calculates the
   Euclidean distance between the two vectors represented by the rows.  A
   and B should have the same dimensions, or else B should have the same
   number of columns in A and one row, in which case the distance from B
   to each row of A is calculated.

 This is performed in a vectorized way, so it should be quite fast.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function distances = distances(A, B)
0002 % distances calculates the distance between each pair of vectors.
0003 %
0004 % Usage:
0005 % distances(A, B)
0006 %   For each row in A, takes the corresponding row in B, and calculates the
0007 %   Euclidean distance between the two vectors represented by the rows.  A
0008 %   and B should have the same dimensions, or else B should have the same
0009 %   number of columns in A and one row, in which case the distance from B
0010 %   to each row of A is calculated.
0011 %
0012 % This is performed in a vectorized way, so it should be quite fast.
0013 %
0014 
0015 if size(A) == size(B)
0016     D = A - B;
0017 else
0018     if size(B, 1) ~= 1
0019         error('The second arg must be the same size as the first, or have one row.');
0020     end
0021     D = bsxfun(@minus, A, B);
0022 end
0023 distances = sqrt(sum(D.*D, 2));

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005