Let's assume that we have an array A
if shape (100,)
and B
of shape (10,)
. Both contain values in [0,1].
How do we get the count of elements in A
greater than each value in B
? I expect an of shape (10,)
, where the first element is "how many in A
are greater than B[0]
", the second is "how many in A
are greater than B[1]
", etc ...
Without using loops.
I tried the following, but it didn't work :
import numpy as np
import numpy.random as rdm
A = rdm.rand(100)
B = np.linspace(0,1,10)
def occ(z: float) ->float:
return np.count_nonzero(A > z)
occ(B)
Python won't use my function as a scalar function on B
, that's why I get:
operands could not be broadcast together with shapes (10,) (100,)
I've also tried with np.greater
but I've got the same issue ...