Python Forum

Full Version: When dividing elements of matrix i would like the result 0 instead of inf?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

While dividing element from first matrix by second matrix value 0 I need result to be 0 instead of inf.

example:
1 1
2 6

divided by

1 0
4 3

should be

1 0(now got inf)
0,5 2
import numpy as np
np.savetxt("result.txt", np.divide(np.loadtxt("1.txt"), np.loadtxt("2.txt")))
m1, m2 = np.loadtxt("1.txt"), np.loadtxt("2.txt")
result = np.divide(m1, m2)
It is normal that division by zero yields infinity, i.e. inf (floating point).
However, you can change all infs to zeros, e.g.
result[np.isinf(result)] = 0.0
(Jul-22-2019, 09:41 AM)scidam Wrote: [ -> ]It is normal that division by zero yields infinity, i.e. inf (floating point).
However, you can change all infs to zeros, e.g.
result[np.isinf(result)] = 0.0

still got inf in result :/
You have to do this after the calculation.
(Jul-22-2019, 11:19 AM)DeaD_EyE Wrote: [ -> ]You have to do this after the calculation.

I think I did:
import numpy as np
np.savetxt("result.txt", np.divide(np.loadtxt("1.txt"), np.loadtxt("2.txt")))
m1, m2 = np.loadtxt("1.txt"), np.loadtxt("2.txt")
result = np.divide(m1, m2)
result[np.isinf(result)] = 0.0
maybe sth else is wrong? I'm not a coder just need this to work so that you for your patience