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.

mergeResultsWithM.m 1.8 KiB

1 week geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function [N, D] = mergeResultsWithM(N1, D1, N2, D2, C1, C2, k, m)
  2. % Merge neighbors from two sources with a limit on candidate neighbors (m)
  3. numQueries = size(N1, 1); % Number of queries in this subset
  4. maxCandidates = min(m, size(N1, 2) + size(N2, 2)); % Maximum candidates to consider
  5. % Combine distances and neighbors
  6. N_combined = [N1, N2 + size(C1, 1)]; % Adjust indices for C2
  7. D_combined = [D1, D2];
  8. % Sort distances and keep only top-m candidates for each query
  9. [D_sorted, idx] = sort(D_combined, 2, 'ascend');
  10. D_sorted = D_sorted(:, 1:maxCandidates); % Keep only top-m distances
  11. idx = idx(:, 1:maxCandidates); % Keep indices corresponding to top-m distances
  12. % Select the corresponding neighbors
  13. %N_sorted = N_combined(sub2ind(size(N_combined), ...
  14. % repmat((1:numQueries)', 1, maxCandidates), idx));
  15. N_sorted = zeros(numQueries, maxCandidates); % Initialize output
  16. for i = 1:numQueries
  17. for j = 1:maxCandidates
  18. N_sorted(i, j) = N_combined(i, idx(i, j));
  19. end
  20. end
  21. % Handle cases where m < k
  22. if maxCandidates < k
  23. % Pad with Inf distances and invalid indices
  24. D_sorted = [D_sorted, Inf(numQueries, k - maxCandidates)];
  25. N_sorted = [N_sorted, zeros(numQueries, k - maxCandidates)];
  26. end
  27. % Extract top-k from the reduced set of candidates
  28. [D, idx_final] = sort(D_sorted, 2, 'ascend');
  29. D = D(:, 1:k); % Final top-k distances
  30. %N = N_sorted(sub2ind(size(N_sorted), ...
  31. % repmat((1:numQueries)', 1, k), idx_final(:, 1:k)));
  32. % Extract top-k neighbors using a loop
  33. N = zeros(numQueries, k); % Initialize output
  34. for i = 1:numQueries
  35. for j = 1:k
  36. N(i, j) = N_sorted(i, idx_final(i, j));
  37. end
  38. end
  39. end