PDS_kNN/matlab/distXY.m

22 lines
724 B
Matlab

function D = distXY(X, Y)
%distXY Calculate an m x n Euclidean distance matrix D of X and Y
%
% Calculate an m x n Euclidean distance matrix D between two set
% points X and Y of m and n points respectively
%
% X : [m x d] Corpus data points (d dimensions)
% Y : [n x d] Query data points (d dimensions)
% D : [m x n] Distance matrix where D(i,j) the distance of X(i,:) and Y(j,:)
[m, d1] = size(X);
[n, d2] = size(Y);
if d1 == d2
d = d1;
else
error('Corpus(X) and Query(Y) data points must have the same dimensions (d)');
end
D = (X.*X) * ones(d,1)*ones(1,n) -2 * X*Y.' + ones(m,1)*ones(1,d) * (Y.*Y).';
%D = sum(X.^2, 2) - 2 * X*Y.' + sum(Y.^2, 2).'
D = sqrt(D);
end