Suppose I have two arrays of data, which I want to divide:
n = 1000;
a = RandomInteger[{1, 9}, {n, n}];
b = RandomInteger[{1, 9}, {n, n}];
First @ AbsoluteTiming @ (a/b)
0.637064
But suppose that two thirds of the values are actually zero. I'd want the ones dividing by zero to just remain zero.
mask = RandomChoice[{0, 0, 1}, {n, n}];
c = mask a;
d = mask b;
First @ AbsoluteTiming @ Quiet[c/d /. Indeterminate -> 0]
8.568857
Now it has become very slow. How can we speed this up?
Edit: Thanks for all the answers guys. Below are some timings: