How to get TRUE/FLASE to work for arrays of cells with mixed string and integer values?

조회 수: 3 (최근 30일)
I have a 1x14 cell array:
data{1} = 9 9 NaN 6 19 8 17 7 15 31 6 18 4 13
I am trying to apply a T/F logic test to it:
tf = data{1} == NaN;
But I get 1x14 vector of zeroes, doesn't seem to work for non-integers.
Whats the best way to check for a string value?
The end goal is to remove the cells containing 'NaN' from the array.
Thank you!

채택된 답변

Matt Fig
Matt Fig 2012년 10월 13일
NaN is not a string, but is the symbol for Not-A-Number. For example, 0/0 will produce NaN. To remove the nans, do something like this:
data{1} = [9 9 NaN 6 19 8 17 7 15 31 6 18 4 13];
data{1} = data{1}(~isnan(data{1}))
If, rather than remove the nans you wish to replace them with something like zero, to this:
data{1} = [9 9 NaN 6 19 8 17 7 15 31 6 18 4 13];
data{1}(isnan(data{1})) = 0
  댓글 수: 1
sono
sono 2012년 10월 17일
I see. I just got thrown in to the wonderful world of matlab; still trying to figure out this craziness.
tank you! that's perfect.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 10월 13일
편집: Walter Roberson 2012년 10월 13일
You need to use isnan() to compare to NaN. NaN does not test equal to anything, including itself.
NaN == NaN
will give false.
You do not show any strings in your sample input.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by