필터 지우기
필터 지우기

How do I delete empty cells in rows of a cell array?

조회 수: 2 (최근 30일)
Tom
Tom 2013년 6월 11일
Hi,
I have a cell array of strings and empty cells. I would like to rearrange this so the empty cells are on the end of each row. For example:
[a] [b] [ ] [ ] [c]
[d] [ ] [e] [ ] [ ]
should become:
[a] [b] [c] [ ] [ ]
[d] [e] [ ] [ ] [ ]
The reason I want to do this is to ultimately end up with a cell matrix of strings in the following form:
[abc]
[de]
How would I accomplish these things, or is there a more efficient way to end up with the matrix of strings?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 11일
편집: Azzi Abdelmalek 2013년 6월 11일
A={['a'] ['b'] [ ] [ ] ['c']
['d'] [ ] ['e'] [ ] [ ]}
idx=not(cellfun(@isempty,A))
out=arrayfun(@(x) cell2mat(A(x,idx(x,:))),[1:size(A,1)]','un',0)

추가 답변 (1개)

Matt J
Matt J 2013년 6월 11일
편집: Matt J 2013년 6월 11일
You don't need to reorder the empty cells if all you intend to do is concatenate across rows:
>> C={'a',[],'b';[], 'e','d'}
C =
'a' [] 'b'
[] 'e' 'd'
>> Ccat=cell(size(C,1),1); for ii=1:size(C,1); Ccat{ii}=[C{ii,:}] ;end,
>> Ccat
Ccat =
'ab'
'ed'

카테고리

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