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.
 
 
 

30 lines
750 B

  1. function [I, D] = kNN(X, Y, k)
  2. %kNN return the k-nearest neighbors Of Y into dataset X
  3. %
  4. % Outputs:
  5. % I : [n x k] The indexes of X where the nearest neighbors of Y lies
  6. % D : [n x k] The distances of each neighbor
  7. %
  8. % Inputs:
  9. % X : [m x d] Corpus data points (d dimensions)
  10. % Y : [n x d] Query data points (d dimensions)
  11. % k : [scalar] The number of neighbors
  12. disMat = distXY(X, Y);
  13. [m, n] = size(disMat);
  14. II = repmat([1:k].', 1, n); % init the min algorithm
  15. DD = disMat(1:k,:);
  16. for i = k+1:m
  17. for j = 1:n
  18. [c, ci] = tail(DD); % calculate candidate and canditate index
  19. if disMat(i,j) < c(j)
  20. DD()
  21. end
  22. end
  23. end
  24. I = II.';
  25. D = DD.';
  26. end