0

The function a[a.size - (a >= 0)[::-1].argmax():] = 0 assigns 0 to the arrays if the array is equal to or less than 0, it also assigns 0 to the arrays that are behind it. In example a the function works fine it assigns 0 to third element till the last element. However in example b it does not do that even though the initial value is negative. How could i modify the function so that it gives me the desired result.

Example A

a = np.array([[  123.7   131.1 -4578.2 -4579.1 -4520.1 -4561.3 -4490.9 -4498.5 -4513.8
 -4550.9]])
a[a.size - (a >= 0)[::-1].argmax():] = 0
Output = [123.7 131.1   0.    0.    0.    0.    0.    0.    0.    0. ]

Example B

b = np.array([-570.,  -593.37,   -286.59771,  -264.24308862])
b[b.size - (b >= 0)[::-1].argmax():] = 0
Output = [-570.,  -593.37,   -286.59771,  -264.24308862]
Desired Output = [0, 0, 0, 0]
1
  • I think the array a has one extra dimension since I copied your code and it didn't give me the output. You might edit that. Commented Feb 8, 2021 at 6:36

1 Answer 1

1

You can do it with a similar approach:

b[(b>=0).argmin():] = 0

(It also works for a)

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

Comments

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.