0

Suppose I have the following array:

a = np.array([0,1,0],
             [1,0,0],
             [0,0,1])

Would it be possible to do something like the this:

a[==0] = -1

to set all zeros to -1? I know you could something like the following to achieve the same effect:

b = a == 0
a[b] = -1

but I was just wondering if there was a way to skip the creation of the Boolean array for this.

1
  • 1
    Not with boolean indexing, which by definition needs that boolean array. But alternatively : 2*a-1 for such an array of 1s and 0s. Commented Oct 31, 2017 at 9:20

2 Answers 2

1

Your syntax was almost correct. This seems to work:

a[a == 0] = -1
Sign up to request clarification or add additional context in comments.

Comments

1

using np.where

np.where(a==0, -1, a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.