'Selecting certain indices from numpy array and adding 2
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9])
How to add 2 to arr[0:2] and arr[5:6] so the final result is:
arr[3,4,3,4,5,8,7,8,9]
Solution 1:[1]
Note that you could create an array that has all the indices to the values that need to be modified:
arr[np.r_[0:2, 5:6]] += 2
print(arr)
Out:
array([3, 4, 3, 4, 5, 8, 7, 8, 9])
Solution 2:[2]
Very straight forward :)
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9])
arr[0:2] += 2
arr[5:6] += 2
print(arr)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | jotjern |
