AUTH's THMMY "Parallel and distributed systems" course assignments.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

25 行
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