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.

recursiveKNN.m 905 B

1234567891011121314151617181920212223242526272829303132
  1. function [neighbors, distances] = recursiveKNN(C, Q, k, m)
  2. % Recursive break, call knnsearch
  3. if size(C, 1) <= 1000 || size(Q, 1) <= 500 % Adjastable limit
  4. [neighbors, distances] = knnsearch(C, Q, 'K', k);
  5. return;
  6. end
  7. % Divide into subsets
  8. midC = floor(size(C, 1) / 2);
  9. midQ = floor(size(Q, 1) / 2);
  10. C1 = C(1:midC, :);
  11. C2 = C(midC+1:end, :);
  12. Q1 = Q(1:midQ, :);
  13. Q2 = Q(midQ+1:end, :);
  14. % Recursive calls
  15. [N1_1, D1_1] = recursiveKNN(C1, Q1, k, m);
  16. [N1_2, D1_2] = recursiveKNN(C2, Q1, k, m);
  17. [N2_1, D2_1] = recursiveKNN(C1, Q2, k, m);
  18. [N2_2, D2_2] = recursiveKNN(C2, Q2, k, m);
  19. % Merge
  20. [N1, D1] = mergeResultsWithM(N1_1, D1_1, N1_2, D1_2, C1, C2, k, m);
  21. [N2, D2] = mergeResultsWithM(N2_1, D2_1, N2_2, D2_2, C1, C2, k, m);
  22. % Combine results for Q1 and Q2
  23. neighbors = [N1; N2];
  24. distances = [D1; D2];
  25. end