Parallel and distributed systems exercise 2: ditributed all-kNN algorithm.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

22 lines
724 B

  1. function D = distXY(X, Y)
  2. %distXY Calculate an m x n Euclidean distance matrix D of X and Y
  3. %
  4. % Calculate an m x n Euclidean distance matrix D between two set
  5. % points X and Y of m and n points respectively
  6. %
  7. % X : [m x d] Corpus data points (d dimensions)
  8. % Y : [n x d] Query data points (d dimensions)
  9. % D : [m x n] Distance matrix where D(i,j) the distance of X(i,:) and Y(j,:)
  10. [m, d1] = size(X);
  11. [n, d2] = size(Y);
  12. if d1 == d2
  13. d = d1;
  14. else
  15. error('Corpus(X) and Query(Y) data points must have the same dimensions (d)');
  16. end
  17. D = (X.*X) * ones(d,1)*ones(1,n) -2 * X*Y.' + ones(m,1)*ones(1,d) * (Y.*Y).';
  18. %D = sum(X.^2, 2) - 2 * X*Y.' + sum(Y.^2, 2).'
  19. D = sqrt(D);
  20. end