When dividing elements of matrix i would like the result 0 instead of inf?
 When dividing elements of matrix i would like the result 0 instead of inf? Koczkodan Unladen Swallow Posts: 3 Threads: 1 Joined: Jul 2019 Reputation: Jul-22-2019, 08:14 AM (This post was last modified: Jul-22-2019, 09:07 PM by Yoriz.) 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 Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: Jul-22-2019, 09:41 AM 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 Koczkodan Unladen Swallow Posts: 3 Threads: 1 Joined: Jul 2019 Reputation: Jul-22-2019, 10:21 AM (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 DeaD_EyE Da Bishop Posts: 1,714 Threads: 6 Joined: May 2017 Reputation: Jul-22-2019, 11:19 AM You have to do this after the calculation. Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply Koczkodan Unladen Swallow Posts: 3 Threads: 1 Joined: Jul 2019 Reputation: Jul-22-2019, 11:40 AM (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 1,574 May-08-2020, 01:41 PM Last Post: nnk Dividing a single column of dataframe into multiple columns based on char length darpInd 2 1,127 Mar-14-2020, 09:19 AM Last Post: scidam dividing by list not possible? SchroedingersLion 5 2,035 Oct-11-2018, 10:00 AM Last Post: volcano63 Checking the elements of a matrix with an elements of a list juniorcoder 11 3,531 Sep-17-2018, 03:02 PM Last Post: gruntfutuk linspace not dividing equal intervals sheel 0 1,666 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