# How to do conditional matrix division when elements are a combination of zero and real numbers?

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

DL= and PL=

By using conditional function:

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

MatrixForm[test]


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?

-
(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

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.

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