Avoid division by zero between matrices in MATLAB [duplicate]
问题 This question already has an answer here : Matlab element-wise division by zero (1 answer) Closed 3 years ago . I'm using matlab and I have two matrices : G = 1 1 1 1 1 1 1 1 and the scond: m = 4 4 4 4 0 0 0 0 I want this result : x = 1/4 1/4 1/4 1/4 0 0 0 0 What I did so far is this : x = G ./ m But it returns : x = 1/4 1/4 1/4 1/4 NaN NaN NaN NaN How can I avoid the divison by zero by placing a default value "0" if there is a division by zero ? 回答1: You can convert the NaNs back to zero: x