Python Forum

Full Version: [numpy]add a value to an array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have an array created with numpy

arr10=np.random.randint(0,10,(4,4))
If for example I want to sum to each element the value 10 where the value is > 5
how I do that please ?
Thank You -
Quote:I want to sum to each element the value 10

I don't understand this part. Would you like to add 10 to every value over 5? In such case you could do something like this:

>>> import numpy as np
>>> arr10=np.random.randint(0,10,(4,4))
>>> arr10
array([[7, 2, 6, 2],
       [5, 7, 0, 8],
       [4, 2, 0, 5],
       [1, 5, 6, 6]])
>>> np.where(arr10 > 5, arr10+10, arr10)
array([[17,  2, 16,  2],
       [ 5, 17,  0, 18],
       [ 4,  2,  0,  5],
       [ 1,  5, 16, 16]])
>>>