How to create a vector of indexes with numpy.where
Question:
I have a vector, and i want to create a new one with indexes of positions where it follows sign change. I have to use numpy.where() , and i don’t know how to create the condtion for sign change, to acces an element and the next one.
Example:
for
v = [1, 2, -1, 2, 3, -1, 3, -10, -10, -10]
return
[1, 2, 4, 5, 6]
Answers:
Use a combination of numpy.diff
and numpy.sign
a = np.array([1, 2, -1, 2, 3, -1, 3, -10, -10, -10])
np.where(np.diff(np.sign(a)))[0].tolist()
I have a vector, and i want to create a new one with indexes of positions where it follows sign change. I have to use numpy.where() , and i don’t know how to create the condtion for sign change, to acces an element and the next one.
Example:
for
v = [1, 2, -1, 2, 3, -1, 3, -10, -10, -10]
return
[1, 2, 4, 5, 6]
Use a combination of numpy.diff
and numpy.sign
a = np.array([1, 2, -1, 2, 3, -1, 3, -10, -10, -10])
np.where(np.diff(np.sign(a)))[0].tolist()