non-iteratively turning a 2d numpy array into a 1d array, via an AND operation

Question:

i have a 2d numpy array of boolean values of shape(1E5, 2), with it acting like an array of x,y values.
ie, 1E5 items each with 2 boolean values, being stored in that initial 2d array

example: [ [True, False], ..., [False, False] ]

i would like to turn this 2d array into a 1d array, where the [True, True] values become True, and all other values are False (an AND operation).

#example: [ [True, True], [False, True], [False, False] ] --> [ True, False, False ]

for the purposes of keeping walltime down, i would like to do this without using a loop. i believe that a one-line solution exists in a form similar to that of a comparison, but i have yet to come up with a solution myself.

#example of what i mean by a comparison: index = positions > self.centre #(here it turns a x,y array into the 2d boolean array i currently have)

ive tried:

quadIndex = positions > self.centre

positions is an array holding the x,y values for 1E5 particles, and self.centre is the x,y coords of the current quadrant of my graph i’m looking at.
creates quadIndex which is a 2d length=1E5 boolean (mask?),
that records whether the x or y position of a particle "passed" the condition`

inQuad = np.any( ( quadIndex == np.bool_( [x, y] ) ) == [True, True] )

this line is part of a nested for loop that goes over the 4 quadrants (BL, TL, BR, TR),
and then uses the boolean values of quadIndex to see if that particle is in that quadrant.
When this check is performed, i then check to see if those particles resulted in a True,True,
and then check to see if there are any True, True’s among the array`

i hoped for "FlowChart":

#are you greater or lesser than the centre? eg return, [ [True, False] ]

#are you in this quadrant? (quadrant is 0,1) so return is [ [False, False] ]

#was that a [True, True]? eg, return, False

#and when the full 1E5 array is passed through this i want to end up with:

#[ True, False, ..., True ]

i currently get:

#are you greater or lesser than the centre? eg return, [ [True, False] ]

#are you in this quadrant? (quadrant is 0,1) so return is [ [False, False] ]

#was that a [True]? eg, return, [False, False]

#full 1E5:

#[ [False, False], [True, False], ..., [True, True] ]

Asked By: Daltem

||

Answers:

Just AND the columns:

new_arr = arr[:, 0] & arr[:, 1]
Answered By: user2357112
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.