Suppose I have a 2D array, such as
a = array([[0,1],[10,12]])
Is there a simple and fast (for huge arrays) way to create a new 2D array aplus1 which adds a constant only to the 2nd axis, while keeping the 1st axis as it is? I thus wish to obtain (for constant = 1)
array([[0,1],[11,13]])
I was hoping that aplus1 = np.add(a,1,axis=1) would be available, but np.add has no option axis=.
CodePudding user response:
IIUC, you can do this by indexing as:
a[row_index, :] = constant_value
So, for modifying a:
a[1, :] = 1 # [[ 0 1] [11 13]]
or if you need a new array, you can copy a at first. So, e.g., if row_index = 1 and constant_value = 2:
b = np.copy(a)
b[1, :] = 2 # [[ 0 1] [12 14]]
