MATLAB Answers


if NaN then...

Asked by Clarisha Nijman on 2 Jan 2019
Latest activity Commented on by Clarisha Nijman on 4 Jan 2019
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.
if Som==1
I find this code clumsy and would prefer a more straight forward code saying:
if sum(sum(PMatrix))=NaN
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.


Sign in to comment.

2 Answers

Steven Lord
Answer by Steven Lord
on 2 Jan 2019
 Accepted Answer

Which release are you using? For release R2018b or later:
if any(isnan(PMatrix), 'all')
For release R2018a or earlier:
if any(isnan(Pmatrix(:)))


Show 1 older comment
Thanks a lot!!!!,
This works for me the best! I agree really straight forward!
Steven Lord
on 3 Jan 2019
As per isakson said, NaN is not equal to any value, not even another NaN. That's by design.
NaN == NaN % returns false
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.

Sign in to comment.

Answer by GT
on 2 Jan 2019
Edited by per isakson
on 3 Jan 2019

  1 Comment

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:
if D==nan
but it does not work. The if statement is the problem. How should it be corrected?

Sign in to comment.