Elena Greg
Elena Greg

Reputation: 1165

How to find the indexes of values that are very close to zero?

How to find the indexes of values except the function[0] that are very closed to zero, please?

import numpy as np
border1 = 0
border2 = 0.5
h = 0.03
N = 1000
t = np.linspace(border1, border2, N)
f = 20
function = h*np.sin(t*f)

index1, = np.where(function == -4.65146680e-05)
index2, = np.where(function == 9.30292241e-05)
print(index1)
print(index2)
print(function)
eps = 1e-1
index, = np.where((function[1:].all() > -eps) and (function[1:].all() < eps))
print(index)

Upvotes: 1

Views: 71

Answers (1)

Chris Mueller
Chris Mueller

Reputation: 6690

You can find the index of the value which is closest to the value you are looking for by using np.argmin.

val1 = -4.65146680e-05
index1 = np.argmin(np.abs(function - val))

Upvotes: 2

Related Questions