Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove or replace matrix in python

Tags:

python

numpy

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. ]]
like image 915
lim bp Avatar asked Mar 09 '26 09:03

lim bp


1 Answers

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]]
like image 184
minhduc0711 Avatar answered Mar 10 '26 21:03

minhduc0711



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!