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

1 vue (au cours des 30 derniers jours)
Tom
Tom le 11 Juin 2013
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?

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 11 Juin 2013
Modifié(e) : Azzi Abdelmalek le 11 Juin 2013
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 commentaire
Tom
Tom le 11 Juin 2013
Modifié(e) : Tom le 11 Juin 2013
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 11 Juin 2013
Modifié(e) : Matt J le 11 Juin 2013
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'

Catégories

En savoir plus sur Cell Arrays 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!

Translated by