from numpy import * m = array([[1,0], [2,3]])
I would like to compute the element-wise log2(m), but only in the places whe
log2(m)
Another option is to use the where parameter of numpy's ufuncs:
m = np.array([[1., 0], [2, 3]]) res = np.log2(m, out=np.zeros_like(m), where=(m!=0))
No RuntimeWarning is raised, and zeros are introduced where the log is not computed.
RuntimeWarning