'Find element in 2d array closest to a specified value within a certain section of that array
I am trying to calculate the gradient of the linear section at the beginning of a line graph that I have plotted from a 2D array. Instead of looking through the array myself to find the values that I need to calculate this gradient, I want code that will do this for me. For example, if it looks like the linear section ends at around 200MPa (The black line here)
I want the code to find the stress value and corresponding index that is nearest to 200 in the first section of the graph (first 2% on the x axis)
The code I have written so far finds the closest value but includes the whole array so gives points near the end of the curve occasionally
def find_nearest(array, value):
diff = np.abs(array - value)
idx = np.where(diff == diff.min())
return idx
valueA = 200
print(find_nearest(TensA, valueA))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

