Removing NaN from matrix with strings and numbers

3 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by