AUTH's THMMY "Parallel and distributed systems" course assignments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

25 lines
810 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. % Calculate the distance matrix between C and Q
  6. % D is an m x n matrix where each element D(i, j) is the distance
  7. % between the i-th point in C and the j-th point in Q.
  8. % k is the number of nearest neighbors to find.
  9. D = dist2(C, Q);
  10. % Find the k-nearest neighbors for each query point in Q
  11. % [~,n] = size(D);
  12. % for j = 1:n
  13. % [dst(:, j), idx(:, j)] = mink(D(:, j), k);
  14. % end
  15. [dst, idx] = mink(D, k, 1); % mink along dimension 1 for each query point
  16. % Transpose the output to match the knnsearch format
  17. idx = idx'; % Make idx an n x k matrix
  18. dst = dst'; % Make dst an n x k matrix
  19. end