title: How to find a unique set of closest pairs of points?
body: <p><code>A</code> and <code>B</code> are sets of <code>m</code> and <code>n</code> points respectively, where <code>m&lt;=n</code>. I want to find a set of <code>m</code> <em>unique</em> points from <code>B</code>, named <code>C</code>, where the sum of distances between all <code>[A(i), C(i)]</code> pairs is the minimal.</p>
  
  <p>To solve this without <em>uniqueness</em> constraint I can just find closest points from <code>B</code> to each point in <code>A</code>:</p>
  
  <pre><code>m = 5; n = 8; dim = 2;
  A = rand(m, dim);
  B = rand(n, dim);
  D = pdist2(A, B);
  [~, I] = min(D, [], 2);
  C2 = B(I, :);
  </code></pre>
  
  <p>Where there may be repeated elements of <code>B</code> present in <code>C</code>. Now the first solution is brute-force search:</p>
  
  <pre><code>minSumD = inf;
  allCombs = nchoosek(1:n, m);
  for i = 1:size(allCombs, 1)
      allPerms = perms(allCombs(i, :));
      for j = 1:size(allPerms, 1)
          ind = sub2ind([m n], 1:m, allPerms(j, :));
          sumD = sum(D(ind));
          if sumD&lt;minSumD
              minSumD = sumD;
              I = allPerms(j, :);
          end
      end
  end
  C = B(I, :);
  </code></pre>
  
  <p>I think <code>C2</code> (set of closest points to each <code>A(i)</code>) is pretty much alike <code>C</code> except for its repeated points. So how can I decrease the computation time?</p>