Simultaneous Changing Of Python Numpy Array Elements
I have a vector of integers from range [0,3], for example: v = [0,0,1,2,1,3, 0,3,0,2,1,1,0,2,0,3,2,1]. I know that I can replace a specific values of elements in the vector by oth
Solution 1:
You can check if v
is equal to zero and then convert the boolean array to int, and so if the original value is zero, the boolean is true and converts to 1, otherwise 0:
v = np.array([0,0,1,2,1,3, 0,3,0,2,1,1,0,2,0,3,2,1])
(v == 0).astype(int)
# array([1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0])
Or use numpy.where
:
np.where(v == 0, 1, 0)
# array([1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0])
Post a Comment for "Simultaneous Changing Of Python Numpy Array Elements"