Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to divide two large matrices DLand PL of size 100,000 x 5 each and they look like this,

DL=enter image description here and PL=enter image description here

By using conditional function:

Spread1[DL_, PL_] := SparseArray[ {i_, j_} /; PL[[i, j]] != 0 :> 100000 DL[[i, j]]/PL[[i, j]], {1000, 5}]

test = Spread1[DL, PL];

and then I get result as shown, but why do I get those messages and some of the resulting test matrix elements say ComplexInfinity? Not sure if I should ignore those messages as all zero elements are aligned?

enter image description here

share|improve this question
(To state the obvious), presumably you are dividing by zero in places. – Daniel Lichtblau Nov 15 '12 at 16:24
@DanielLichtblau I think that's why there's a !=0 test in there. Shouldn't that take care of it? – tkott Nov 15 '12 at 20:14
up vote 1 down vote accepted

First, make up some data that has approximately the form you are looking for:

z = RandomChoice[{0, 1}, {10, 5}];
dl = RandomVariate[NormalDistribution[], {10, 5}] z;
pl = RandomVariate[NormalDistribution[], {10, 5}] z;

What you want to do is to divide dl by pl. Of course this isn't really well defined because you are trying to divide zero by zero. So, you have to make a choice about what you want this to be. If you want the answer to 0/0 to be zero, then you can calculate

 dl/(pl //. {0. -> 1})

On the other hand, if you prefer 0./0. to be 1, then try

 (dl //. {0. -> 1})/(pl //. {0. -> 1})

In case it isn't clear, what these are doing is to replace all the zeros in the matrix with "1" in order to avoid the divide by zero errors.

share|improve this answer
Hi @bill s thanks I will use this later. – sebastian c. Nov 16 '12 at 1:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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