How to obtain the best pairwise mapping from one set to another

I have a set of one dimensional points in a vector A, and another set of points in a vector B. I need to pair up points in A with their nearest equivalent points in B - without allowing any points to be in more than one pair.
Sounded simple, but it's proved more difficult that I thought.
I've tried knnsearch(), but this just maps A to nearest B and allows duplicates. I've looked at clustering but this does not seem to distinguish the set A from set B.
Seems like their should be an established way to do this...?

Answers (1)

My solution:
A = randi(50,1,10) % Origin Vector
B = randi(50,1,10) % ‘Target’ Vector
Bc = B; % ‘Target’ Vector for Comparisons
for k1 = 1:size(Bc,2)
[~,I] = min(abs(A(k1) - Bc)); % Find Nearest B to A(k1)
D(k1,:) = [A(k1) k1 B(I) I]; % Store Result
Bc(I) = NaN; % Use ‘NaN’ to Prevent Repeats
end
Q = D(:,[1 3]) % Output — [A, Closest *Remaining* Value in B]
There may be better and more efficient ways to do this, but I can’t think of them this morning.

Asked:

on 9 Aug 2014

Answered:

on 9 Aug 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!