Issue
I have an array A
. I would like to divide the non-zero elements and obtain a new array B
. The desired output is attached.
import numpy as np
A=np.array([[0,1,2],[4,0,8],[7,5,0]])
print([A])
The desired output is
B=[array([[0,1/1,1/2],[1/4,0,1/8],[1/7,1/5,0]])]
Solution
Using numpy.where
(you will get a RuntimeWarning
though):
B = np.where(A, 1/A, 0)
or numpy.divide
:
B = np.divide(1, A, out=np.zeros_like(A, dtype=float), where=A!=0)
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.