Cell array / Concatenate non - NaN results
조회 수: 10 (최근 30일)
이전 댓글 표시
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
Alex Mcaulley
2019년 4월 10일
is that an exeption? Are there more different cells? I mean, for example:
'[a;b]'
'[a,b;c,d]'
채택된 답변
Guillaume
2019년 4월 10일
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개)
Alex Mcaulley
2019년 4월 10일
A(cellfun(@isnan,A)) = [];
댓글 수: 2
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))))=[];
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!