function D = distXY(X, Y) %distXY Calculate an m x n Euclidean distance matrix 𝐷 of X and Y % % Calculate an m x n Euclidean distance matrix 𝐷 between two sets points 𝑋 and 𝑌 of 𝑚 and 𝑛 points respectively % X : [m x d] Corpus data points (d dimensions) % Y : [n x d] Query data poinsts (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 have to have the same dimensions'); 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).' end