0

I've searched around but can't seem to find any efficient way to select a portion of a 3d array depending on indices. Lets say for example that I have some 3d array with dimensions 200 x 200 x 200 and I want to select and change the value of all elements where all indices are greater than 100

import numpy as np

mask = np.ones((200,200,200))

for x in np.arange(0,mask.shape[0]):
    for y in np.arange(0,mask.shape[1]):
        for z in np.arange(0,mask.shape[2]):
            if x > 100 & y > 100 & z > 100:
                mask[x,y,z] = 0
            else:
                mask[x,y,z] = 1

There must be some efficient way to do this using np.select or similar but I just can't get my head around it. Any help would be much appreciated.

1 Answer 1

4

I want to select and change the value of all elements where all indices are greater than 100

The following should do it:

mask[101:,101:,101:] = 0
Sign up to request clarification or add additional context in comments.

1 Comment

Strictly speaking, that will change the values where all indices are greater than or equal to 100.

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.