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

  • $\begingroup$ (To state the obvious), presumably you are dividing by zero in places. $\endgroup$ – Daniel Lichtblau Nov 15 '12 at 16:24
  • $\begingroup$ @DanielLichtblau I think that's why there's a !=0 test in there. Shouldn't that take care of it? $\endgroup$ – tkott Nov 15 '12 at 20:14

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.

  • $\begingroup$ Hi @bill s thanks I will use this later. $\endgroup$ – sebastian c. Nov 16 '12 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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