title: Numpy matrix binarization using only one expression
body: <p>I am looking for a way to binarize numpy N-d array based on the threshold using only one expression. So I have something like this:</p>
  
  <pre><code>np.random.seed(0)
  np.set_printoptions(precision=3)
  a = np.random.rand(4, 4)
  threshold, upper, lower = 0.5, 1, 0
  </code></pre>
  
  <p>a is now:</p>
  
  <pre><code>array([[ 0.02 ,  0.833,  0.778,  0.87 ],
         [ 0.979,  0.799,  0.461,  0.781],
         [ 0.118,  0.64 ,  0.143,  0.945],
         [ 0.522,  0.415,  0.265,  0.774]])
  </code></pre>
  
  <p>Now I can fire these 2 expressions:</p>
  
  <pre><code>a[a&gt;threshold] = upper
  a[a&lt;=threshold] = lower
  </code></pre>
  
  <p>and achieve what I want:</p>
  
  <pre><code>array([[ 0.,  1.,  1.,  1.],
         [ 1.,  1.,  0.,  1.],
         [ 0.,  1.,  0.,  1.],
         [ 1.,  0.,  0.,  1.]])
  </code></pre>
  
  <p>But is there a way to do this with just one expression?</p>