0

Let say we create this np.array:

A = np.arange(12).reshape(3, 4)

So, A is:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

I would like to assign some values to A like that:

A[[0,2]][:,[1,3]] = 9999

In order to obtain:

array([[ 0,  9999,  2,  9999],
       [ 4,  5,  6,  7],
       [ 8,  9999, 10, 9999]])

But this doesn't work. What is the proper way to do it?

Thanks

1 Answer 1

1

You can use np.ix_ to get those open meshes, which when used for indexing into the array would be broadcasted and thus could be used for assigning values into it, like so -

A[np.ix_([0,2],[1,3])] = 9999
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.