Replace NaN with specific value.

조회 수: 35 (최근 30일)
Mehul Agrawal
Mehul Agrawal 2016년 6월 27일
댓글: Steven Lord 2016년 6월 27일
I have many large column matrices (each is ~20k x 1). Some elements of the matrices are NaN. I want to replace them with 0 (or some number). Is there a quick way for that. Looping through each element is a very inefficient process and delays my program. The help is really appreciated.

채택된 답변

Guillaume
Guillaume 2016년 6월 27일
편집: Guillaume 2016년 6월 27일
yourmatrix(isnan(yourmatrix)) = somevalue;
But, using NaN to mark exceptional values is much better than using 0 or some other special value.
  댓글 수: 2
Mehul Agrawal
Mehul Agrawal 2016년 6월 27일
Thanks a lot. I actually wanted to do some additions of these matrices and having NaN ignores all other calculations and I will end up having NaN. For my purpose 0 is actually a unique value too (just eligible for calculations).
Steven Lord
Steven Lord 2016년 6월 27일
For a couple releases now the sum function has accepted a flag that lets you specify whether NaN values should be included in the sum (which makes the sum NaN) or excluded (unless all the values being summed are NaN.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by