Find all local Maxima and Minima when x and y values are given as numpy arrays

前端 未结 3 515
日久生厌
日久生厌 2020-12-03 08:16

I have two arrays x and y as :

x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])
3条回答
  •  无人及你
    2020-12-03 08:57

    This will work fine.

    Python uses += instead of ++.

    Before you use i in a while loop you have to assign some value - in this case 0 - , this way initializing it to avoid error.

    import numpy as np
    
    x=np.array([6,3,5,2,1,4,9,7,8])
    y=np.array([2,1,3,5,3,9,8,10,7])
    
    
    sortId=np.argsort(x)
    x=x[sortId]
    y=y[sortId]
    minm = np.array([])
    maxm = np.array([])
    i = 0
    while i < y.size-1:
       while(y[i+1] >= y[i]):
          i+=1
    
       maxm=np.insert(maxm,0,i)
       i+=1
       while(y[i+1] <= y[i]):
          i+=1
    
       minm=np.insert(minm,0,i)
       i+=1
    
    print minm, maxm
    

提交回复
热议问题