when given array = [0.771, 0.812, 0.812, 0.847, 0.847, 0.847, 0.849, 0.847, 0.887, 0.862, 0.863, 0.892, 0.874, 0.874, 0.899, 0.945,0.910, 0.872, 0.887, 0.874, 0.872, 0.896, 0.896, 0.860, 0.912, 0.913, 0.912, 0.871, 0.871, 0.865, 0.899, 0.866]
I want to find a maximum value by using sliding window algorithm.(Not by using np.max())
n = len(array) k = 12 (which is the window size)
Here is the code:
for i in range(n-k):
max = arr[i]
for j in range (1, k):
if arr[i+j] >= max:
max = arr[i+j]
print(str(max) + "", end="")
this will give output
= 0.892 0.892 0.892 0.899 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.913 0.913 0.913 0.913
According to the output, we can see that the maximum value is 0.945. However, I want to stop the loop iteration when the last element of the output is smaller than the previous last element.
0.892 0.892 0.892 0.899 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.945 0.913 0.913 0.913 0.913
The bolded number (0.913) is where I want to stop the for loop iteration.
can anyone please help me how to code this part? Thank you