I have some lists of data that I need to divide by each-other, but my problem is that for some values the denominator is zero. Now these particular values are not very important, but I still need to keep them in the lists.

To solve my problem, I tried defining my own division function:


This works great for single numbers,

out: -1

but not for lists:

out: {ComplexInfinity,2,0}

How do I make my predefined output work for lists as well?

Of course, another solution to my problem would be to iterate over the list with e.g. a For loop and an If, but I think there should be a neater way of doing this.


2 Answers 2

SetAttributes[division, Listable]
division[{1, 2, 0}, {0, 1, 2}]

{-1, 2, 0}


Another possibility is to use Block:

division[a_, b_] := Quiet @ Block[{DirectedInfinity},
    Divide[a, b]

division[{1, 2, 0}, {0, 1, 2}]

{-1, 2, 0}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.