Right Array Division : Ignoring division by zeroes

前端 未结 1 617
南笙
南笙 2021-01-20 03:45

I have two data matrices A and B of similar dimensions. I intend to divide each element of A by its corresponding elements of B<

相关标签:
1条回答
  • 2021-01-20 04:20

    Yes. You can use logical indexing:

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

    With logical indexing, only the elements of A, B and C corresponding to true elements of t will be evaluated. t is true only where B is non-zero. Note that C is pre-initialized to zeros to automatically take care of the cases that are not evaluated because B is zero.

    0 讨论(0)
提交回复
热议问题