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.
 
 
 

31 lines
832 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 j = 1:n
  17. for i = k+1:m
  18. % calculate candidate and canditate index
  19. [tail, taili] = maxIdx(DD(:, j));
  20. if disMat(i,j) < tail
  21. DD(taili, j) = disMat(i,j);
  22. II(taili, j) = i;
  23. end
  24. end
  25. end
  26. I = II.';
  27. D = DD.';
  28. end