Mathematical division of Matlab by zero

I have two matrices, for example X = [1 2; 3 4; 5 6] and Y = [0 1; -eleven; eleven]. I want to do element separation X./Y, but I need a way to ignore all zeros in Y.

I tried using something like:

nonzeros = find (Y ~ = 0); X (non-zero) ./ Y (non-zero);

but the result has become a column vector, and I need the shape of the result matrix so that it is the same as X (or Y), and with zeros, where Y is zero. Therefore, my desired result for this case is [0 2; -3 4; 5 6].

I also tried what was suggested here ( Right Array Division: Ignoring division by zero ), but at the same time the result again becomes a column vector.

thank

+4
source share
1

-

out = X./Y      %// Perform the elementwise division
out(Y==0)=0     %// Select the positions where Y is zero and 
                %// set those positions in the output to zero

-

X =
     1     2
     3     4
     5     6
Y =
     0     1
    -1     1
     1     1
out =
     0     2
    -3     4
     5     6
+8

All Articles