I have a matrix, R2
. I am summing up the inverse of non-zero elements across each row. However, there is occurrence of spurious values (1st and 5th values) as shown in the current output. How can I avoid this?
R2=array([[0.00000000e 00, 1.86647223e 10, 0.00000000e 00, 6.54826235e 09,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[1.11453512e 10, 0.00000000e 00, 2.82017381e 11, 0.00000000e 00,
3.64932878e 10, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 1.86647223e 10, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 4.48759847e 09, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[1.11453512e 10, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
3.64932878e 10, 0.00000000e 00, 1.87125503e 09, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 1.86647223e 10, 0.00000000e 00, 6.54826235e 09,
0.00000000e 00, 4.48759847e 09, 0.00000000e 00, 4.08814184e 10,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 0.00000000e 00, 2.82017381e 11, 0.00000000e 00,
3.64932878e 10, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
1.65770709e 11, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 6.54826235e 09,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 4.08814184e 10,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
3.64932878e 10, 0.00000000e 00, 1.87125503e 09, 0.00000000e 00,
1.65770709e 11, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 4.48759847e 09, 0.00000000e 00, 4.08814184e 10,
0.00000000e 00, 1.20130268e 10, 3.53068596e 10, 0.00000000e 00],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
1.65770709e 11, 0.00000000e 00, 0.00000000e 00, 5.22617991e 08],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
1.65770709e 11, 0.00000000e 00, 0.00000000e 00, 5.22617991e 08],
[0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 0.00000000e 00, 0.00000000e 00, 0.00000000e 00,
0.00000000e 00, 1.20130268e 10, 3.53068596e 10, 0.00000000e 00]])
R3=np.divide(1, R2, where=R2 != 0).sum(1)
The current output is
array([8.94213159e 130, 1.20671688e-010, 2.76413345e-010, 6.51526508e-010,
7.71108578e 140, 3.69806090e-011, 1.77173260e-010, 5.67835429e-010,
3.58863402e-010, 1.91947593e-009, 1.91947593e-009, 5.22617991e 008])
CodePudding user response:
For doing so, you can do this in two ways, one is by using out=R2
inside your np.devide
as:
np.divide(1, R2, where=R2!=0, out=R2).sum(1)
# R2:
# [2.0628927806128987e-10 1.2067168750757737e-10 2.7641334517657591e-10
# 6.5152650788289563e-10 4.5358660476653588e-10 3.6980608990547905e-11
# 1.7717325958995994e-10 5.6783542936586616e-10 3.5886340231599305e-10
# 1.9194759328959448e-09 1.9194759328959448e-09 1.1156607561074707e-10]
Or put np.devide
result in another variable named R3
and then sum on it as:
R3 = np.divide(1, R2, where=R2!=0)
result = R3.sum(1)