0

I have a numpy array and I want to find all the indexes that verifies a certain condition. Example, I want to plot the Heaviside function;

import numpy as np
x=np.linspace(-5,5,11)
k_neg=x.find(x<0)
k_pos=x.find(x>=0)
y=np.zeros(len(x))
y(k_neg)=-1
y(k_pos)=1

I don't find such a function (like it exists on Matlab).

Note : my actual problem IS NOT to plot Heavyside, of corse ;)

1
  • 2
    Sounds like you are looking for np.where Commented Apr 30, 2018 at 14:38

2 Answers 2

1

As said by Paul Panzer;

Sounds like you are looking for np.where

Which solved my problem.

Sign up to request clarification or add additional context in comments.

Comments

0

I would do it in one line with numpy:

import numpy as np
x = np.linspace(-5,5,11)
y = ((x>=0)*2)-1

2 Comments

OP clearly states Heaviside is only a toy example. They want something more general.
I think this adapts to the given description but if you have a better idea please just post your solution!

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.