title: Python: compare and insert a vector with an ordered numpy array
body: <p>I have a numpy array whose rows is ordered and a vector. I need to insert this vector into the array if the value of the vector is smaller than some values of  each array row. Furthermore, I need to get the index of the position. Can I use a vector like operation rather than two for loops?</p>
  
  <p>For example, the i-th row of the array is [1,3,5,6], and the i-th value of vector is 4, the result is ([1,3,4,5], 2).
  Here is the code example.</p>
  
  <pre><code>import random
  import numpy as np
  
  random.seed(0)
  num_query = 2
  topk = 3
  
  distance = np.Inf * np.ones((num_query, topk))
  
  print(distance)
  
  for iter in range(4):
      print('------------loop: {:d}------------'.format(iter))
      dist = np.random.randn(num_query) / (0.3 * (iter+1))
      dist = dist * np.sign(dist)
      rows, columns = distance.shape
      for row in range(rows):
          d = dist[row]
          if d &gt; distance[row, columns - 1]:
              continue
          min_col = 0
          for column in range(columns-1, -1, -1):
              if distance[row, column] &lt; d:
                  min_col = column + 1
                  break
          distance[row, min_col + 1:] = distance[row, min_col:-1]
          distance[row, min_col] = d
      print(dist)
      print(distance)
      print('+++++++++++++++++++++++++++++++')
  print(distance)
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>[[inf inf inf]
   [inf inf inf]]
  ------------loop: 0------------
  [3.60155727 0.0991411 ]
  [[3.60155727        inf        inf]
   [0.0991411         inf        inf]]
  +++++++++++++++++++++++++++++++
  ------------loop: 1------------
  [1.4839764  1.99641269]
  [[1.4839764  3.60155727        inf]
   [0.0991411  1.99641269        inf]]
  +++++++++++++++++++++++++++++++
  ------------loop: 2------------
  [2.44132044 0.19221564]
  [[1.4839764  2.44132044 3.60155727]
   [0.0991411  0.19221564 1.99641269]]
  +++++++++++++++++++++++++++++++
  ------------loop: 3------------
  [0.29206705 1.17013653]
  [[0.29206705 1.4839764  2.44132044]
   [0.0991411  0.19221564 1.17013653]]
  +++++++++++++++++++++++++++++++
  [[0.29206705 1.4839764  2.44132044]
   [0.0991411  0.19221564 1.17013653]]
  </code></pre>