I have a 2D symmetric matrix (M) that contains 0,1,2
[[1. 2. 0. 0.]
[2. 1. 2. 0.]
[0. 2. 1. 2.]
[0. 0. 2. 1.]]
I want to examin that: set atom i,j in M to 0 where j in i+1, i+2, i+3 to zero in M
I have this code, that do this:
for i in range(len(M)):
for j in range(len(M)):
if M[i, j] != 0:
if j == len(M) - 3:
M[i, j+2] = 0
M[i, j+1] = 0
elif j == len(M) - 2:
M[i, j+1] = 0
elif j == len(M) - 1:
continue
else:
M[i, j + 3] = 0
M[i, j + 2] = 0
M[i, j + 1] = 0
else:
continue
It works pretty well, but i want to reduce my if statements. I read about M[i,j:j+3], but i dont really know how to use it.
I'm waiting the following output matrix:
[[1. 0. 0. 0.]
[2. 0. 0. 0.]
[0. 2. 0. 0.]
[0. 0. 2. 0.]]
Thank you for your help.
Aucun commentaire:
Enregistrer un commentaire