0

I have a 2 dimensional array in numpy and need to apply a mathematical formula just to some values of the array which match certain criteria. This can be made using a for loop and if conditions however I think using numpy where() method works faster.

My code so far is this but it doesn't work

cond2 = np.where((SPN >= -alpha) & (SPN <= 0))
SPN[cond2] = -1*math.cos((SPN[cond2]*math.pi)/(2*alpha))

The values in the orginal array need to be replaced with the corresponding value after applying the formula.

Any ideas of how to make this work? I'm working with big arrays so need and efficient way of doing it. Thanks

1 Answer 1

3

Try this:

cond2 = (SPN >= -alpha) & (SPN <= 0)
SPN[cond2] = -np.cos(SPN[cond2]*np.pi/(2*alpha))
Sign up to request clarification or add additional context in comments.

2 Comments

Might as well call np.pi as well so everything comes from the same library.
@Hooked yes I missed it ... :)

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.