title: Using numpy.where to prevent out of bound
body: <p>I am trying to look up values in an array based on an array of indices. This array of indices can contain indices that could be out of bound. In that case I want to return a particular value (here 0).</p>
  
  <p>(I could use a for loop but that would be too slow.)</p>
  
  <p>So I am doing this:</p>
  
  <pre><code>data = np.arange(1000).reshape(10, 10, 10)
  i = np.arange(9).reshape(3, 3)
  i[0, 0] = 10
  condition = (i[:, 0] &lt; 10) &amp; (i[:, 1] &lt; 10) &amp; (i[:, 2] &lt; 10)
  values = np.where(condition, data[i[:, 0], i[:, 1], i[:, 2]], 0)
  </code></pre>
  
  <p>However I still get an out of bound error:</p>
  
  <pre><code>IndexError: index 10 is out of bounds for axis 0 with size 10
  </code></pre>
  
  <p>I guess it is because the second parameters is not lazily evaluated and is evaluated before the function call.</p>
  
  <p>Is there a solution in numpy to access an array based on a condition but still retain the order? By retaining the order I mean that I can't filter the array first because I could loose the order in the end result. In the end, in that particular example, I still want the values array to contain a 0 when the indices are out of bound. So the end result would be:</p>
  
  <pre><code>array([ 0, 345, 678])
  </code></pre>