24 lines
558 B
Python
24 lines
558 B
Python
def find_maxima(x):
|
|
"""Find local maxima of x.
|
|
|
|
Input arguments:
|
|
x -- 1D list of real numbers
|
|
|
|
Output:
|
|
idx -- list of indices of the local maxima in x
|
|
"""
|
|
idx = []
|
|
for i in range(len(x)):
|
|
if i == 0 and x[i] > x[i+1]:
|
|
idx.append(i)
|
|
elif i != len(x)-1 and x[i] > x[i-1] and x[i] > x[i+1]:
|
|
idx.append(i)
|
|
elif i== len(x)-1 and x[i] > x[i-1]:
|
|
idx.append(i)
|
|
return idx
|
|
|
|
|
|
if __name__ == "__main__":
|
|
x = [1, 2, 3]
|
|
# x = [1,2,2,3,1]
|
|
print(find_maxima(x))
|