Exclusion of region in numpy array

2567 views python
1

I have a numpy array (dat) of shape (n,3) where n denotes the number of rows, and the three columns represent the x (column 0), y (column 1) and z (column 2) coordinates respectively.

I want to EXCLUDE those rows in this numpy array where the values of x lie between a certain limit ( xlow < x < xupp AND where the values of y lie between ylow < x < yupp AND where the values of z lie between zlow < x < zupp. I already have values of [xlow, xupp, ylow, yupp, zlow, zupp].

I know how to find the region (rows) where the condition of exclusion holds using:

mark = np.where( ( dat[:,0]>xlow & \
                   dat[:,0]<xhigh )  & \
                 ( dat[:,1]>ylow & \
                   dat[:,1]<yhigh ) & \
                 ( dat[:,2]>zlow & \
                   dat[:,2]<zhigh ) )

But, I want these rows to be excluded in my new array. How can I do this in numpy? Thanks.

answered question

Drop the where and use the not operator ~.

Do you mean np.~(....)? I get error (invalid syntax) when I do that.

1 Answer

11

Try taking the negation of the condition inside np.where(). Like this:

mark = np.where( ~( ( dat[:,0]>xlow & \
                     dat[:,0]<xhigh )  & \
                   ( dat[:,1]>ylow & \
                     dat[:,1]<yhigh ) & \
                   ( dat[:,2]>zlow & \
                     dat[:,2]<zhigh ) ) )

posted this

Have an answer?

JD

Please login first before posting an answer.