0

I have created a boolean mask, say mask, which I want to apply to an existing array, say old to create an entirely new one, say new, which retains only the non zero elements. The new array should then have a smaller dimension with respect to old.

Can some one suggest me the fastest and more coincise way, without using, if possible, the numpy.append function?

1
  • Please create an MCVE with inputs, and desired output Commented Dec 4, 2018 at 11:56

2 Answers 2

1

Say you have:

old = np.array([2,4,3,5,6])
mask = [True, False, True, False, False]

Simply do:

new = old[mask]
print(new)
[2 3]

I suggest you read about Boolean or “mask” index arrays

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

Comments

0

Just use logical indexing

x = x[x!=0]

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.