/usr/share/octave/packages/statistics-1.3.0/pdist2.m is in octave-statistics 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | ## Copyright (C) 2014 Piotr Dollar <pdollar@gmail.com>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 3 of the
## License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see
## <http:##www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {} pdist2 (@var{x}, @var{y})
## @deftypefnx {Function File} {} pdist2 (@var{x}, @var{y}, @var{metric})
## Compute pairwise distance between two sets of vectors.
##
## Let @var{X} be an MxP matrix representing m points in P-dimensional space
## and @var{Y} be an NxP matrix representing another set of points in the same
## space. This function computes the M-by-N distance matrix @var{D} where
## @code{@var{D}(i,j)} is the distance between @code{@var{X}(i,:)} and
## @code{@var{Y}(j,:)}.
##
## The optional argument @var{metric} can be used to select different
## distances:
##
## @table @asis
## @item @qcode{"euclidean"} (default)
##
## @item @qcode{"sqeuclidean"}
## Compute the squared euclidean distance, i.e., the euclidean distance
## before computing square root. This is ideal when the interest is on the
## order of the euclidean distances rather than the actual distance value
## because it performs significantly faster while preserving the order.
##
## @item @qcode{"chisq'"}
## The chi-squared distance between two vectors is defined as:
## @code{d(x, y) = sum ((xi-yi)^2 / (xi+yi)) / 2}.
## The chi-squared distance is useful when comparing histograms.
##
## @item @qcode{"cosine"}
## Distance is defined as the cosine of the angle between two vectors.
##
## @item @qcode{"emd"}
## Earth Mover's Distance (EMD) between positive vectors (histograms).
## Note for 1D, with all histograms having equal weight, there is a simple
## closed form for the calculation of the EMD. The EMD between histograms
## @var{x} and @var{y} is given by @code{sum (abs (cdf (x) - cdf (y)))},
## where @code{cdf} is the cumulative distribution function (computed
## simply by @code{cumsum}).
##
## @item @qcode{"L1"}
## The L1 distance between two vectors is defined as: @code{sum (abs (x-y))}
##
## @end table
##
## @seealso{pdist}
## @end deftypefn
## Taken from Piotr's Computer Vision Matlab Toolbox Version 2.52, with
## author permission to distribute under GPLv3
function D = pdist2 (X, Y, metric = "euclidean")
if (nargin < 2 || nargin > 3)
print_usage ();
elseif (columns (X) != columns (Y))
error ("pdist2: X and Y must have equal number of columns");
elseif (ndims (X) != 2 || ndims (Y) != 2)
error ("pdist2: X and Y must be 2 dimensional matrices");
endif
switch (tolower (metric))
case "sqeuclidean", D = distEucSq (X, Y);
case "euclidean", D = sqrt (distEucSq (X, Y));
case "l1", D = distL1 (X, Y);
case "cosine", D = distCosine (X, Y);
case "emd", D = distEmd (X, Y);
case "chisq", D = distChiSq (X, Y);
otherwise
error ("pdist2: unknown distance METRIC %s", metric);
endswitch
D = max (0, D);
endfunction
## TODO we could check the value of p and n first, and choose one
## or the other loop accordingly.
## L1 COMPUTATION WITH LOOP OVER p, FAST FOR SMALL p.
## function D = distL1( X, Y )
## m = size(X,1); n = size(Y,1); p = size(X,2);
## mOnes = ones(1,m); nOnes = ones(1,n); D = zeros(m,n);
## for i=1:p
## yi = Y(:,i); yi = yi( :, mOnes );
## xi = X(:,i); xi = xi( :, nOnes );
## D = D + abs( xi-yi' );
## end
function D = distL1 (X, Y)
m = rows (X);
n = rows (Y);
mOnes = ones (1, m);
D = zeros (m, n);
for i = 1:n
yi = Y(i,:);
yi = yi(mOnes,:);
D(:,i) = sum (abs (X-yi), 2);
endfor
endfunction
function D = distCosine (X, Y)
p = columns (X);
X = X ./ repmat (sqrt (sumsq (X, 2)), [1 p]);
Y = Y ./ repmat (sqrt (sumsq (Y, 2)), [1 p]);
D = 1 - X*Y';
endfunction
function D = distEmd (X, Y)
Xcdf = cumsum (X,2);
Ycdf = cumsum (Y,2);
m = rows (X);
n = rows (Y);
mOnes = ones (1, m);
D = zeros (m, n);
for i=1:n
ycdf = Ycdf(i,:);
ycdfRep = ycdf(mOnes,:);
D(:,i) = sum (abs (Xcdf - ycdfRep), 2);
endfor
endfunction
function D = distChiSq (X, Y)
## note: supposedly it's possible to implement this without a loop!
m = rows (X);
n = rows (Y);
mOnes = ones (1, m);
D = zeros (m, n);
for i = 1:n
yi = Y(i, :);
yiRep = yi(mOnes, :);
s = yiRep + X;
d = yiRep - X;
D(:,i) = sum (d.^2 ./ (s+eps), 2);
endfor
D = D/2;
endfunction
function dists = distEucSq (x, y)
xx = sumsq (x, 2);
yy = sumsq (y, 2)';
dists = bsxfun (@plus, xx, yy) - 2 * x * (y');
endfunction
## euclidean distance as loop for testing purposes
%!function dist = euclidean_distance (x, y)
%! [m, p] = size (X);
%! [n, p] = size (Y);
%! D = zeros (m, n);
%! for i = 1:n
%! d = X - repmat (Y(i,:), [m 1]);
%! D(:,i) = sumsq (d, 2);
%! endfor
%!endfunction
%!test
%! x = [1 1 1; 2 2 2; 3 3 3];
%! y = [0 0 0; 1 2 3; 0 2 4; 4 7 1];
%! d = sqrt([ 3 5 11 45
%! 12 2 8 30
%! 27 5 11 21]);
%! assert (pdist2 (x, y), d)
|