function [N, D] = mergeResults(N1, D1, N2, D2, C1, C2, k, ~) % Merge neighbors from two sources for a specific subset of queries numQueries = size(N1, 1); % Queries corresponding to N1 (or N2) N_combined = [N1, N2 + size(C1, 1)]; % Adjust indices for C2 D_combined = [D1, D2]; % Sort distances and select top-k for each query [D_sorted, idx] = sort(D_combined, 2); % Sort by distance for each query idx = idx(:, 1:k); % Top-k indices % Select corresponding neighbors N = zeros(numQueries, k); for i = 1:numQueries N(i, :) = N_combined(i, idx(i, :)); end % Select corresponding distances D = D_sorted(:, 1:k); end