if NaN then...

조회 수: 266 (최근 30일)
Clarisha Nijman
Clarisha Nijman 2019년 1월 2일
댓글: Clarisha Nijman 2019년 1월 4일
Dear all,
I have a function that returns me a PMatrix. But sometimes that matrix is full with nan's. The cause is known, but the best solution I found is to allow the function to compute such kind of "nan" matrices and to replace that "NaN matrix in the end by the identity matrix.
This is my actual code that works fine, uptill now.
Som=sum(sum(PMatrix));
Som(isnan(Som))=1;
if Som==1
PMatrix=eye(k,k);
end
I find this code clumsy and would prefer a more straight forward code saying:
if sum(sum(PMatrix))=NaN
PMatrix=eye(k,k)
end
But I can't figure out how to compose such a straight forward code. Does anyone has a suggestion for me?
Thank you in advance.

채택된 답변

Steven Lord
Steven Lord 2019년 1월 2일
Which release are you using? For release R2018b or later:
if any(isnan(PMatrix), 'all')
end
For release R2018a or earlier:
if any(isnan(Pmatrix(:)))
end
  댓글 수: 4
Steven Lord
Steven Lord 2019년 1월 3일
As per isakson said, NaN is not equal to any value, not even another NaN. That's by design.
NaN == NaN % returns false
Clarisha Nijman
Clarisha Nijman 2019년 1월 4일
Oh, now I see, it can not be seen as a number nor a string. By the way, your second suggestion worked for me! I accidently omiited the colon (:). It should be right in the place where you put it.

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

추가 답변 (1개)

GT
GT 2019년 1월 2일
편집: per isakson 2019년 1월 3일
  댓글 수: 1
Clarisha Nijman
Clarisha Nijman 2019년 1월 3일
편집: per isakson 2019년 1월 3일
Yes, but I do not want to transfer the entries in ones, It would imply a matrix full of ones. I just need the identity matrix. I figured already out that sum(sum(P))==nan. So an if loop saying:
D+sum(sum(P))==nan
if D==nan
P=eye(k)
end
but it does not work. The if statement is the problem. How should it be corrected?

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

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by