title: Using NumPy argsort and take in 2D arrays
body: <p>The aim is to calculate the distance matrix between two sets of points (<code>set1</code> and <code>set2</code>), use <code>argsort()</code> to obtain the sorted indexes and <code>take()</code> to extract the sorted array. I know I could do a <code>sort()</code> directly, but I need the indexes for some next steps.</p>
  
  <p>I am using the fancy indexing <a href="https://stackoverflow.com/q/14491480/832621">concepts discussed here</a>. I could not manage to use <code>take()</code> directly with the obtained matrix of indexes, but adding to each row a corresponding quantity makes it work, because <code>take()</code> flattens the source array making the second row elements with an index += len(set2), the third row index += 2*len(set2) and so forth (see below):</p>
  
  <pre><code>dist  = np.subtract.outer( set1[:,0], set2[:,0] )**2
  dist += np.subtract.outer( set1[:,1], set2[:,1] )**2
  dist += np.subtract.outer( set1[:,2], set2[:,2] )**2
  a = np.argsort( dist, axis=1 )
  a += np.array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
                 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
                 [20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
                 [30, 30, 30, 30, 30, 30, 30, 30, 30, 30]])
  s1 = np.sort(dist,axis=1)
  s2 = np.take(dist,a)
  np.nonzero((s1-s2)) == False
  #True # meaning that it works...
  </code></pre>
  
  <p>The main question is: is there a direct way to use <code>take()</code> without summing these indexes?</p>
  
  <p>Data to play with:</p>
  
  <pre><code>set1 = np.array([[ 250., 0.,    0.],
                   [ 250., 0.,  510.],
                   [-250., 0.,    0.],
                   [-250., 0.,    0.]])
  
  set2 = np.array([[  61.0, 243.1, 8.3],
                   [ -43.6, 246.8, 8.4],
                   [ 102.5, 228.8, 8.4],
                   [  69.5, 240.9, 8.4],
                   [ 133.4, 212.2, 8.4],
                   [ -52.3, 245.1, 8.4],
                   [-125.8, 216.8, 8.5],
                   [-154.9, 197.1, 8.6],
                   [  61.0, 243.1, 8.7],
                   [ -26.2, 249.3, 8.7]])
  </code></pre>
  
  <p>Other related questions:</p>
  
  <p><a href="https://stackoverflow.com/a/1871630/832621">- Euclidean distance between points in two different Numpy arrays, not within</a></p>