PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to get the minimum and maximum of each period in the list?

 June 26, 2022     arrays, graph, list, numpy, python     No comments   

Issue

This is the graph that I got in the list enter image description here

My question is how to get each minimum and maximum of each period in the list? Following the graph I want to get the 1st maximum at point 1, 1st minimum at point 30, 2nd maximum at point 62, 2nd minimum at point 94

I have tried but it got the wrong point since the value in the list is not stable as you can see on the graph

This is my code that I have tried

max_1 = 0
max_1_pos = 0
min_1_pos = 0
max_2 = 0
max_2_pos = 0
min_2_pos = 0
img_list_pix =[218, 225, 224, 224, 224, 220, 217, 215, 216, 216,
               216, 215, 214, 215, 214, 214, 213, 210, 210, 205,
               207, 204, 205, 201, 200, 201, 201, 197, 202, 203,
               196, 196, 196, 196, 198, 198, 199, 202, 202, 205,
               205, 205, 207, 206, 208, 207, 209, 214, 212, 213,
               215, 218, 220, 219, 218, 220, 223, 222, 224, 224,
               224, 222, 225, 222, 222, 222, 222, 223, 223, 221,
               223, 222, 223, 219, 218, 216, 215, 214, 212, 212,
               211, 211, 207, 206, 205, 204, 202, 199, 199, 198,
               197, 195, 193, 191, 191, 193, 191, 196]

for i in range(len(img_list_pix)):
    if img_list_pix[i] >= max_1:
        max_1 = img_list_pix[i]
        max_1_pos = i
    else:
        break
min_1 = max_1
print(f"max_1:{max_1} | i:{i}")

for i in range(max_1_pos + 1, len(img_list_pix)):
    if img_list_pix[i] <= min_1:
        min_1 = img_list_pix[i]
        min_1_pos = i
    else:
        break
max_2 = min_1
print(f"min_1:{min_1} | i:{i}")

for i in range(min_1_pos + 1, len(img_list_pix)):
    if img_list_pix[i] >= max_2:
        max_2 = img_list_pix[i]
        max_2_pos = i
    else:
        break
min_2 = max_2
print(f"max_2:{max_2} | i:{i}")

for i in range(max_2_pos + 1, len(img_list_pix)):
    if img_list_pix[i] <= min_2:
        min_2 = img_list_pix[i]
        min_2_pos = i
    else:
        break
print(f"min_2:{min_2} | i:{i}")

The results of the aforementioned code is:

max_1:225 | i:2
min_1:215 | i:8
max_2:216 | i:11
min_2:214 | i:13

Thanks in Advance.


Solution

Your modified code will be as:

F1 = 0
F1_lim = 15
for i in range(len(img_list_pix)):
    if img_list_pix[i] > max_1:
        max_1 = img_list_pix[i]
        max_1_pos = i
        F1 = 0
    elif F1 <= F1_lim:
        F1 += 1
        if img_list_pix[i] > max_1:
            max_1 = img_list_pix[i]
            max_1_pos = i
            F1 = 0
    else:
        F1 = 0
        break
print(f"max_1:{max_1} | i:{max_1_pos}")

min_1 = max_1
for i in range(max_1_pos + 1, len(img_list_pix)):
    if img_list_pix[i] < min_1:
        min_1 = img_list_pix[i]
        min_1_pos = i
        F1 = 0
    elif F1 <= F1_lim:
        F1 += 1
        continue
    else:
        F1 = 0
        break
print(f"min_1:{min_1} | i:{min_1_pos}")

max_2 = min_1
for i in range(min_1_pos + 1, len(img_list_pix)):
    if img_list_pix[i] > max_2:
        max_2 = img_list_pix[i]
        max_2_pos = i
        F1 = 0
    elif F1 <= F1_lim:
        F1 += 1
        if img_list_pix[i] > max_2:
            max_2 = img_list_pix[i]
            max_2_pos = i
            F1 = 0
    else:
        F1 = 0
        break
print(f"max_2:{max_2} | i:{max_2_pos}")

min_2 = max_2
for i in range(max_2_pos + 1, len(img_list_pix)):
    if img_list_pix[i] < min_2:
        min_2 = img_list_pix[i]
        min_2_pos = i
        F1 = 0
    elif F1 <= F1_lim:
        F1 += 1
        continue
    else:
        F1 = 0
        break
print(f"min_2:{min_2} | i:{min_2_pos}")

F1_lim is number of indices after index of the founded value to compare with, and can be changed by the user. This code will gives the following results:

max_1:225 | i:1
min_1:196 | i:30
max_2:225 | i:62
min_2:191 | i:93

The code will gives the first occurrence of the minimum or maximum values for valleys or peaks, e.g. for min_2 it will return 93. For getting the last one with the same magnitude as 93 i.e. 96 for min_2 in the specified range F1_lim = 15, less than and greater than operators must be substituted by less than equal and greater than equal operators.



Answered By - Ali_Sh
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing