6

Using Numpy, I can create a 5-dimensional array like this:

>>> faces = numpy.zeros((3, 3, 3, 6, 3))

I want (all indexes, all indexes, 0, 4) to be set to (1., 1., 1.). Is this possible by only using Numpy (no Python loops)?

2 Answers 2

6

Both of the following will do it:

faces[:,:,0,4] = 1

faces[:,:,0,4] = (1, 1, 1)

The first uses the fact that all three values are the same by having NumPy broadcast the 1 to the correct dimensions.

The second is a little bit more general in that you can assign different values to the three elements.

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

2 Comments

Is there a way I can put the slice notation in a tuple to pass to Numpy somehow?
@Synthead, You can use slice(None), slice(None), 0, 4
2

Numpy slice notation supports doing this directly

f[:,:,0,4] = 1.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.