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
A
B
C = A./B
C
NAN
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 .
true
t