Python Forum
When dividing elements of matrix i would like the result 0 instead of inf?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When dividing elements of matrix i would like the result 0 instead of inf?
#1
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)
Reply
#2
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
Reply
#3
(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 :/
Reply
#4
You have to do this after the calculation.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 905 May-08-2020, 01:41 PM
Last Post: nnk
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 553 Mar-14-2020, 09:19 AM
Last Post: scidam
  dividing by list not possible? SchroedingersLion 5 1,254 Oct-11-2018, 10:00 AM
Last Post: volcano63
  Checking the elements of a matrix with an elements of a list juniorcoder 11 2,050 Sep-17-2018, 03:02 PM
Last Post: gruntfutuk
  linspace not dividing equal intervals sheel 0 1,153 Jan-16-2018, 04:28 PM
Last Post: sheel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020