CutePoison
CutePoison

Reputation: 5355

Numpy elementwise greater (for each element in another array)

I struggle to write find the best "question" so please feel free to suggest another title.

Lets say I have a=np.array([5,3,2,4]) and b=np.array([1,2]) - I want to get a list of list (or np.arrays) with the value of a>b[i] i.e it can be written as a list comprehension

[a[i]>p for p in b] which returns

[np.array([True,True,True,True]), np.array([True,True,False,True])]. Since I have a rather large data set I hoped that there was a numpy function to do such, or is list comprehension the better way here?

Upvotes: 1

Views: 465

Answers (1)

Blckknght
Blckknght

Reputation: 104712

You can use numpy broadcasting for this, you just need to add an extra dimension into each of your arrays:

>>> a[None,:] > b[:,None]
array([[ True,  True,  True,  True],
       [ True,  True, False,  True]])

Upvotes: 6

Related Questions