0

i want to find the smallest "y" number between idx 2-7, but theres something im not doing right. For the moment it prints x = 0.02 and y = 101, i want it to print out x = 0.05 and y = 104. Even if i change the "idx = 3" to a higher number nothing changes.

I have changed it from max to min, therefor some still say max, but i dont think that matters as long as " y[:idx].argmin()" is min?

import numpy as np
# idx:           0     1     2     3     4     5     6     7
x = np.array([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08]) # strain
y = np.array([ 110,  101,  110,  106,  102,  104,  112,  115]) # load


idx = 3
cutoff = 0.08
while x[idx] < cutoff:
    idx = idx + 1

max_idx = y[:idx].argmin()
max_x = x[max_idx]
max_y = y[max_idx]
print (max_x)
print (max_y)

1 Answer 1

3

y[:idx] are the first idx values. You want y[2:].

Also, min_idx = y[2:].argmin() gives you the min index with respect to y[2:]. So the min index with respect to y would be 2+min_idx.


import numpy as np
# idx:           0     1     2     3     4     5     6     7
x = np.array([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08]) # strain
y = np.array([ 110,  101,  110,  106,  102,  104,  112,  115]) # load

min_idx = y[2:].argmin()
min_x = x[2+min_idx]
min_y = y[2+min_idx]
print (min_x)
# 0.05

print (min_y)
# 102

If you wish to restrict attention to those values of x for which x >= 0.03 and x < 0.07, then use a boolean mask to restrict x and y to those values:

import numpy as np
# idx:           0     1     2     3     4     5     6     7
x = np.array([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08]) # strain
y = np.array([ 110,  101,  110,  106,  102,  104,  112,  115]) # load

lower, upper = 0.03, 0.07
mask = (x >= lower) & (x < 0.07)
# array([False, False,  True,  True,  True,  True, False, False], dtype=bool)

# select those values of x and y
masked_y = y[mask]
masked_x = x[mask]

# find the min index with respect to masked_y
min_idx = masked_y.argmin()

# find the values of x and y restricted to the mask, having the min y value
min_x = masked_x[min_idx]
min_y = masked_y[min_idx]

print (min_x)
# 0.05

print (min_y)
# 102
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, but is there a way to set a "limit" depending on the x value, both lower and upper. Like from 0.03-0.07?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.