hsuominen
hsuominen

Reputation: 1

Selecting portion of multi-dimensional numpy array

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.

Upvotes: 0

Views: 840

Answers (1)

NPE
NPE

Reputation: 500913

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

Upvotes: 4

Related Questions