필터 지우기
필터 지우기

Removing NaN from matrix with strings and numbers

조회 수: 4 (최근 30일)
Diogo Queirós
Diogo Queirós 2015년 1월 29일
댓글: Jan 2018년 6월 28일
I'm having trouble removing NaN from this matrix
cabos=
[1x26 char] 'Início' 'Fim' [1x20 char] 'N. volt. level [KV]'
[2.0088e+03] 'SE S M Portuzelo' 'PTD 0526' [ 18.6057] [ 15]
[ 169.0442] 'PTC 5452' 'PTD 0450' [ 18.6057] [ 15]
[ 446.3509] 'PTD 0039' 'PTC 2850' [ 18.6057] [ 15]
[ 635.2552] 'PTD 0450' 'PTD 0039' [ 18.6057] [ 15]
[ 255.5464] 'PTD 0450' 'PTD 0090' [ 18.6057] [ 15]
[4.2281e+03] 'PTD 0526' 'PTC 5452' [ 18.6057] [ 15]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
I tried this code and it removes de NaN values, but it completely rearanges the matrix and i can't solve this
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []
  댓글 수: 5
Diogo Queirós
Diogo Queirós 2015년 1월 29일
it's a really big project i'm working on and it's all in portuguese soi'm going to try to explain myself better. i'm extracting from an excel file a table, call it A.
A=
[1x26 char] 'Início' 'Fim' [1x20 char] [ NaN]
[2.0088e+03] 'SE S M Portuzelo' 'PTD 0526' [ 18.6057] [ NaN]
[ 169.0442] 'PTC 5452' 'PTD 0450' [ 18.6057] [ NaN]
[ 446.3509] 'PTD 0039' 'PTC 2850' [ 18.6057] [ NaN]
[ 635.2552] 'PTD 0450' 'PTD 0039' [ 18.6057] [ NaN]
[ 255.5464] 'PTD 0450' 'PTD 0090' [ 18.6057] [ NaN]
[4.2281e+03] 'PTD 0526' 'PTC 5452' [ 18.6057] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
[ NaN] [ NaN] [ NaN] [ NaN] [ NaN]
note that the table is much larger, i just took a sample.
all my attempts got me that same error
Undefined function 'isnan' for input arguments of type 'cell'
unless this one that removed all of the NaN but completely disformed my matrix
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []
Guillaume
Guillaume 2015년 1월 29일
편집: Guillaume 2015년 1월 29일
If your code
cabos(cellfun(@(cabos) any(isnan(cabos)),cabos)) = []
does not give you the error Undefined function 'isnan' for input arguments of type 'cell', then neither would mine.
I suspect you've tried Stalin's code, whose first line modified your cabos cell array. And yes, after that, on that my code (but yours as well) you would get the error.
Reload your cabos from file or do:
cabos = cabos{1};

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

채택된 답변

Guillaume
Guillaume 2015년 1월 29일
Possibly, this is what you want. But see comments to your question.
cabos(cellfun(@(c) isscalar(c) && isnan(c), cabos)) = {[]}
This will replace NaNs with empty matrices. The benefit of which is not obvious to me.
  댓글 수: 9
VBBV
VBBV 2018년 6월 28일
Hi ... I am currently working on similar problem, however, I want to replace NaN in double/logical array with character 'A' to appear ... how can I do it ... Many thanks for any help
Jan
Jan 2018년 6월 28일
@Vasishta Bhargava: Please do not hijack a thread by appending a new question as comment to an answer. Open a new thread instead.
Then ths answer will be: This cannot work. You cannot insert characters in a numerical or logical array. All arrays, except for cell arrays, must have the same data type for all elements.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by