Right Array Division: Ignoring Zero Division

I have two data matrices Aand Bsimilar sizes. I intend to split each element Ainto its corresponding elements B. For this, matlab provides a shortcut like C = A./B. However, it Bhas many null elements, for such elements I want the elements Cto be zero, not NAN. Does MATLAB provide a way to do this in an efficient way? I could do it in a loop, but it would be very expensive. Thank you

+3
source share
1 answer

Yes. You can use logical indexing:

C = zeros(size(A));
t = logical(B);
C(t) = A(t)./B(t);

A, B C, true t. t true, B . , C , , , B .

+3

All Articles