alec.tu
alec.tu

Reputation: 1757

place values by condition with numpy

I have a three channel matrix and I want to place the value which is less than 27

a=numpy.arange(27).reshape(3,3,3)
a[a<27]=0

However, If I want to replace only on first channel, the way I can do is to write a for loop

for i in range(3):
    for j in range(3):
        if a[i][j][0] < 27:
            a[i][j][0]=0

I am not sure how to do this with a more simple way.

thank you

Upvotes: 0

Views: 54

Answers (2)

Oresto
Oresto

Reputation: 335

I think, you were looking for this: a[:,:,0][a[:,:,0]<27]=0

Upvotes: 1

Pedro
Pedro

Reputation: 293

You can try with:

a=numpy.arange(27).reshape(3,3,3)
a[a[:,:,0]<27, 0]=0

Upvotes: 1

Related Questions