Cell array / Concatenate non - NaN results

Dear all,
I have a cell array that looks like this :
A = {
[NaN]
[NaN]
'a'
'[b c]'
[NaN]
'b' }
what I would like to obtain is this array without NaNs, and all "non-Nan" values concatenated, i.e.
B = {
'a'
'b'
'c'
'b'}
I would like to avoid doing different "for" loops, I mean doing it in the shortest way possible :)
I'm unsure on how to obtain this..
Thanks in advance !

4 件のコメント

Guillaume
Guillaume 2019 年 4 月 10 日
編集済み: Guillaume 2019 年 4 月 10 日
Are the non-NaN cells really char vectors or are they actually numeric arrays?
If they really are char vectors, then a) why have you got numeric values (NaNs) mixed with text? and b) we need more details of what should happen to text with multiple characters in a cell.From your example it would appear that spaces and brackets need to be removed, what other characters? and that the remaining characters need to be split into individual cells. An odd thing to want.
If the cells are actually numeric arrays. What is their shape? Are they row vectors or scalar as your example would imply or can they be matrices, column vectors, or any shape really?
Eara Eara
Eara Eara 2019 年 4 月 10 日
The problem is my data actually looks exactly like that, and I cannot change the way it is generated (alas !).
When there is a cell looking like this :
'[b c]'
Actually yes, I want to remove the [ and spaces, and make it different elements in my result.
Alex Mcaulley
Alex Mcaulley 2019 年 4 月 10 日
is that an exeption? Are there more different cells? I mean, for example:
'[a;b]'
'[a,b;c,d]'
Eara Eara
Eara Eara 2019 年 4 月 10 日
No, non-NaN values can only look like this :
'a'
or that :
'[b c]'
or maybe
'[b c d]'
But the "regex" will always have one of these forms

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2019 年 4 月 10 日

0 投票

You mention a regex. You'd be better off fixing the regex that produces that output. But if that's not an option:
A = {
[NaN]
[NaN]
'a'
'[b c]'
[NaN]
'b' }
regexp([a{:}], '[a-z]', 'match')
The concatenation of the cells + conversion to char gets rid of the NaNs anyway, so it's just a matter of extracting the characters.

1 件のコメント

Eara Eara
Eara Eara 2019 年 4 月 11 日
Many thanks to both of you @Guillaume and @Alex, I eventually managed to do what I wanted !

サインインしてコメントする。

その他の回答 (1 件)

Alex Mcaulley
Alex Mcaulley 2019 年 4 月 10 日

0 投票

A(cellfun(@isnan,A)) = [];

2 件のコメント

Eara Eara
Eara Eara 2019 年 4 月 10 日
I tried this but it gives me an error
Error using cellfun
Non-scalar in Uniform output, at index 4, output 1.
Set 'UniformOutput' to false.
so I tried this
A(cellfun(@isnan,A, 'UniformOutput',0)) = [];
But it also gives me an error which is
Function 'subsindex' is not defined for values of class 'cell'.
Plus I'm not sure it does what I want with non-Nans values..
Alex Mcaulley
Alex Mcaulley 2019 年 4 月 10 日
編集済み: Alex Mcaulley 2019 年 4 月 10 日
Try this instead: (this code is to remove the nan values, for non nan values we need more information as Guillaume said)
A(logical(cellfun(@sum,cellfun(@isnan,A,'UniformOutput',false))))=[];

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2019 年 4 月 10 日

コメント済み:

2019 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by