title: NumPy: Find sorted indices from a masked 2D array above and below a threshold
body: <p>I have a 2D masked array of values that I need to sort from lowest to highest. For example:</p>
  
  <pre><code>import numpy as np
  
  # Make a random masked array
  &gt;&gt;&gt; ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                       mask=np.random.binomial(1, .2, 20)).reshape((4,5))
  &gt;&gt;&gt; print(ar)
  [[-- 51.9 38.3 46.8 43.3]
   [52.3 65.0 51.2 46.5 --]
   [56.7 51.1 -- 38.6 33.5]
   [45.2 56.8 74.1 58.4 56.4]]
  
  # Sort the array from lowest to highest, with a flattened index
  &gt;&gt;&gt; sorted_ind = ar.argsort(axis=None)
  &gt;&gt;&gt; print(sorted_ind)
  [14  2 13  4 15  8  3 11  7  1  5 19 10 16 18  6 17  0 12  9]
  </code></pre>
  
  <p>But with the sorted indices, I need to divide them into two simple subsets: <em>less than or equal to</em> and <em>greater than or equal to</em> a given datum. Furthermore, I don't need the masked values, and they need to be removed. For example, with <code>datum = 51.1</code>, how do I filter down <code>sorted_ind</code> to the 10 indices above <code>datum</code> and 8 values below? (Note: there is one shared index due to the <em>or equal to</em> logic criteria. The 3 masked values can be removed from analysis). I need to preserve the flattened index position, as I use <code>np.unravel_index(ind, ar.shape)</code> later on.</p>