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]
Asked By: Lucian Schipor

||

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()
Answered By: bitflip
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.