arr = array([[ 14., 48.],
[ 18., 45.],
[ 9., 42.],
[ 9., 57.],
[ 3., 30.]])
arr[:,0] += 1
if you need a copy, copy the array first with arr.copy()
.
Question
I have an array of the form (just an example):
array([[ 14., 48.],
[ 18., 45.],
[ 9., 42.],
[ 9., 57.],
[ 3., 30.]])
And I want to increment by 1 the first component of all the elements to get this:
array([[ 15., 48.],
[ 19., 45.],
[ 10., 42.],
[ 10., 57.],
[ 4., 30.]])
Right now, I am using this to do so:
arr = np.array([arr.T[0] + 1, arr.T[1]]).T
But I was wondering if there was a better way to manipulate subarrays like this. Is there?
Solution
arr = array([[ 14., 48.],
[ 18., 45.],
[ 9., 42.],
[ 9., 57.],
[ 3., 30.]])
arr[:,0] += 1
if you need a copy, copy the array first with arr.copy()
.