#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Numpy: array division but only for non-zero values


    Hi all,

    I am using numpy n-dimensional arrays for some image processing and one thing I am doing is dividing element by element between two arrays.

    So the code looks something like this:

    Code:
    import numpy as np
    ....
    value = np.sum(np.absolute(cc_num/cc_den))
    where cc_num and cc_den are numpy arrays. The problem is some of the values in the cc_den can be 0 and I do not want to perform the division operation on those array elements.

    What is the pythonisque way to achieve this? Any help would be greatly appreciated.

    Thanks,
    Luca
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0
    I found the solution. It is as follows:

    Code:
    nonzero = cc_den > 0
    value = np.sum(np.absolute(cc_num[nonzero]/cc_den[nonzero]))

IMN logo majestic logo threadwatch logo seochat tools logo