Replacing all NaNs with zeros without looping through the whole matrix?

前端 未结 3 653
臣服心动
臣服心动 2020-12-06 17:42

I had an idea to replace all the NaNs in my matrix by looping through each one and using isnan. However, I suspect this will make my code run more slowly than it should. Can

3条回答
  •  时光取名叫无心
    2020-12-06 18:13

    Let's say your matrix is:

    A = 
         NaN   1       6
         3     5     NaN
         4     NaN     2
    

    You can find the NaN elements and replace them with zero using isnan like this :

    A(isnan(A)) = 0;
    

    Then your output will be:

    A =
         0     1     6
         3     5     0
         4     0     2
    

提交回复
热议问题