title: Using arrays in an if statement condition
body: <p>How do I use arrays in the condition part of an <code>if</code> statement? I want the program to check every element's absolute value, and return the appropriate part. This gave some hope: <a href="https://stackoverflow.com/questions/8036878/function-of-numpy-array-with-if-statement">Function of Numpy Array with if-statement</a><br/>
  But this technique isn't working in my case.
  Here is the code I'm trying: <br/></p>
  
  <pre><code>    def v(x,y):
          if x[abs(x)&lt;a] and y[abs(y)&lt;a]: 
              return #something 1
          if x[a&lt;abs(x)&lt;b] and y[a&lt;abs(y)&lt;b]:
              return #something 2
          if x[b&lt;abs(x)&lt;R] and y[b&lt;abs(y)&lt;R]:
              return #something 3
  </code></pre>
  
  <p>Here, x and y are arrays. (Actually a grid created by <code>x,y = ogrid[-R:R:.01, -R:R:.01]</code>)
  <br/><br/>
  Edit: The best way I found (after much trial and error) is to use boolean arrays. Here's the code:
  <br/></p>
  
  <pre><code>    #Create a grid of coordinates. XY-Plane.
      x,y=ogrid[-R:R+1:1,-R:R+1:1] 
  
      #Create the polar distance 2D array. Conditions are tested on this array.    
      r=sqrt(x**2+y**2)     
  
      #Create 2D array to hold the result
      v=zeros([len(x*y),len(x*y)],float)
  
      #idr is the boolean 2D array of same size and shape as r. 
      #It holds truth values wherever the condition is true.
      idr=(r&lt;a)
      v[~invert(idr)]= #function 1 of r[~invert(idr)]
  
      idr=((r&gt;=a)&amp;(r&lt;b))
      v[~invert(idr)]= #function 2 of r[~invert(idr)]
  
      idr=(r&gt;=b)&amp;(r&lt;=R)
      v[~invert(idr)]= #function 3 of r[~invert(idr)]
  
      print v
  </code></pre>
  
  <p><br/>The values in v get updated at the correct locations.<br/>
  Thanks for the help!</p>