title: Find all local Maxima and Minima when x and y values are given as numpy arrays
body: <p>I have two arrays <code>x</code> and <code>y</code> as :</p>
  
  <pre><code>x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
  y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])
  </code></pre>
  
  <p>I am finding index of local minima and maxima as follows:</p>
  
  <pre><code>sortId = np.argsort(x)
  x = x[sortId]
  y = y[sortId]
  minm = np.array([])
  maxm = np.array([])
  while i &lt; y.size-1:
     while(y[i+1] &gt;= y[i]):
        i = i + 1
  
     maxm = np.insert(maxm, 0, i)
     i++
     while(y[i+1] &lt;= y[i]):
        i = i + 1
  
     minm = np.insert(minm, 0, i)
     i++
  </code></pre>
  
  <p>What is the problem in this code?
  The answer should be index of <code>minima = [2, 5, 7]</code>
  and that of <code>maxima = [1, 3, 6]</code>.</p>