Matlab element-wise division by zero

前端 未结 1 1936
时光取名叫无心
时光取名叫无心 2020-12-19 11:45

I have two matrices, say X = [1 2; 3 4; 5 6] and Y = [0 1; -1 1; 1 1]. I want to perform element-wise division X./Y, but I need a way to ignore all the zeros in Y.

相关标签:
1条回答
  • 2020-12-19 12:14

    Use this -

    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
    

    Output -

    X =
         1     2
         3     4
         5     6
    Y =
         0     1
        -1     1
         1     1
    out =
         0     2
        -3     4
         5     6
    
    0 讨论(0)
提交回复
热议问题