Let x be a numeric vector. In Matlab typing:
y=1*(x>0.1)
Gives a vector of the same size as x with ones and zeros depending on whether the corresponding element in x satisfies the condition. Is there a similar short hand statement in Python (or common library) to achieve this without a loop?
CodePudding user response:
y = [ int(z > 1) for z in x]
CodePudding user response:
This could be done with Numpy but I can't think of a standard Python method of doing this without a loop.
For example:
import numpy as np
import random
x = np.array([random.random() for _ in range(50)])
y = np.array(x > 0.1, dtype=int)
will yield an array of y
that meets your requirements.