Reputation: 11039
I use NumPy.
I have defined a vector x
with NumPy and other variables with numerical values.
I will return a vector y
of same length as x
but the values y[i]
in this vector y
need to be computed from different formulas depending on the corresponding x[i]
.
Can I with NumPy do something smart or do I have to iterate through the vector x
and for each element in x
determine if x[i]
is either greater than or less than a specific value and determine which formula to use for the specific element?
I guess I could do something like
y[x > a] = 2*x+7
y[x <= a] = 3*x+9
return y
Upvotes: 5
Views: 123
Reputation: 6606
Check out np.where
http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html.
y = np.where(x > a, 2 * x + 7, 3 * x + 9)
Upvotes: 6