AUTH's THMMY "Parallel and distributed systems" course assignments.
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.
 
 
 
 
 

29 line
1017 B

  1. function [idx, dst] = knnsearch2(C, Q, k)
  2. % C: Is a MxD matrix (Corpus)
  3. % Q: Is a NxD matrix (Query)
  4. % k: The number of nearest neighbors needded
  5. % idx: Is the Nxk matrix with the k indexes of the C points, that are
  6. % neighbors of the nth point of Q
  7. % dst: Is the Nxk matrix with the k distances to the C points of the nth
  8. % point of Q
  9. %
  10. % Calculate the distance matrix between C and Q
  11. % D is an m x n matrix where each element D(i, j) is the distance
  12. % between the i-th point in C and the j-th point in Q.
  13. % k is the number of nearest neighbors to find.
  14. D = dist2(C, Q);
  15. % Find the k-nearest neighbors for each query point in Q
  16. % [~,n] = size(D);
  17. % for j = 1:n
  18. % [dst(:, j), idx(:, j)] = mink(D(:, j), k);
  19. % end
  20. [dst, idx] = mink(D, k, 1); % mink along dimension 1 for each query point
  21. % Transpose the output to match the knnsearch format
  22. idx = idx'; % Make idx an n x k matrix
  23. dst = dst'; % Make dst an n x k matrix
  24. end