Cell array / Concatenate non - NaN results
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Alex Mcaulley
le 10 Avr 2019
is that an exeption? Are there more different cells? I mean, for example:
'[a;b]'
'[a,b;c,d]'
Réponse acceptée
Guillaume
le 10 Avr 2019
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.
Plus de réponses (1)
Alex Mcaulley
le 10 Avr 2019
A(cellfun(@isnan,A)) = [];
2 commentaires
Alex Mcaulley
le 10 Avr 2019
Modifié(e) : Alex Mcaulley
le 10 Avr 2019
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))))=[];
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!