Reputation: 31
Hi i have this matrix
e.g
import numpy as np
a= np.arange(9).reshape(3, 3)
[[1. 2. 3. ]
[4. 5. 6. ]
[7. 8. 9. ]]
how do i delete the bottomost value of a selected col (e.g col = 1) and the rest of the values on top gets pushed down, and if there is an empty space, put "0"
so the matrix becomes
[[1. 0. 3. ]
[4. 2. 6. ]
[7. 5. 9. ]]
Upvotes: 3
Views: 79
Reputation: 244
I suggest you may use np.pad
here:
E.g.
>>> import numpy as np
>>> a= np.arange(1, 10).reshape(3, 3)
>>> print a
[[1 2 3]
[4 5 6]
[7 8 9]]
>>> a[:,1] = np.pad(a[:,1], pad_width=1, mode='constant')[:a.shape[1]]
>>> print a
[[1 0 3]
[4 2 6]
[7 5 9]]
Upvotes: 0
Reputation: 231475
In [215]: a = np.arange(1,10).reshape(3,3)
Make a temporary array to hold the new column's values:
In [216]: temp = np.zeros(a.shape[0],a.dtype)
In [217]: temp[1:] = a[:-1,1]
copy that into a
:
In [218]: a[:,1] = temp
In [219]: a
Out[219]:
array([[1, 0, 3],
[4, 2, 6],
[7, 5, 9]])
Upvotes: 0
Reputation: 25
Roll the second column and then set the first element of the column to zero
a = np.arange(1,10).reshape(3, 3)
a[:, 1] = np.roll(a[:, 1], 1)
a[0, 1] = 0
print(a)
Output:
[[1 0 3]
[4 2 6]
[7 5 9]]
Upvotes: 1