1

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

jwodder
  • 54,758
  • 12
  • 108
  • 124
park null
  • 11
  • 3
  • You do realize that this will fail to find the blobab maximum when the overall maximum occurs at a point after the local maximum is smaller than the global max so far? – tripleee Mar 01 '21 at 12:17
  • Your edit just made your question make absolutely no sense. Did you delete the text of the question by accident? – RufusVS Mar 06 '21 at 03:44

2 Answers2

0

Create another variable which contains the global max, and quit when the new local max is lower.

gl_max = arr[0]
broken = False
for i in range(n-k):
    loc_max = arr[i]
    for j in range (1, k):
        if arr[i+j] >= loc_max:
            loc_max = arr[i+j]
            if loc_max < gl_max:
                broken = True
                break
            gl_max = loc_max
    if broken:
        break
    print(loc_max, end="")

This requires some additional logic to break out of both nested loops. If this is inside a function, return instead of the innermost break, and remove the broken logic. See also How to break out of multiple loops?

Perhaps also notice the removal of unnecessary chaff out of the print, and the renaming of the variable to avoid shadowing the built-in max function as well as clarifying the difference between the global vs local max variables.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0
globalValue = arr[0]
flag = False
for i in range(n-k):
    max = arr[i]
    if flag:
        break
    for j in range (1, k):
        if arr[i+j] >= max:
            max = arr[i+j]
    if max>=globalValue:
        print(str(max) + "", end="")
        globalValue = max
    else:
        print(str(max) + "", end="")
        flag = True

globalValue will store the current value printed and will get updated.flag will check whether a lower value is printed or not. With this, you can solve your problem.

Shovo
  • 133
  • 2
  • 9