title: sort Numpy array elements by value
body: <p>Say I have an NxN array of values, and I want to sort these values by binning them, and for each bin, return a copy of the original with only the corresponding values remaining and all others replaced by zero. For example, if I have:</p>
  
  <pre><code>x = np.array([[1.1,2.2],[1.2,3.2]])
  bins = np.array([1,2,3,4])
  </code></pre>
  
  <p>Then I want:</p>
  
  <pre><code>output = [[[1.1,0.0],[1.2,0.0]],[[0.0,2.2],[0.0,0.0]],[[0.0,0.0],[0.0,3.2]]]
  </code></pre>
  
  <p>What I have so far:</p>
  
  <pre><code>Z = np.shape(x)[0]
  Y = np.shape(x)[1]
  X = np.shape(bins)[0]-1
  output = np.broadcast_to(x, (X,Y,Z))
  </code></pre>
  
  <p>This creates an array which contains a copy of the original for each bin. Then I figure I would use np.where to set all but the correct values to 0.0, but my mind blanks when I try to think about how exactly to write it, and I'm not sure it's the best way in particular either.</p>
  
  <p>Any suggestions would be appreciated, thank you.</p>