How do I remove the empty cells from a vector of cells?
Ältere Kommentare anzeigen
I have a vector of cells which contain strings. Some of the cells in the vector are empty. I want to remove the empty cells from the vector.
Suppose I start with
strs = {'one','','two','three','','','four',''};
and I want to end with
strs = {'one','two','three','four'};
2 Kommentare
Rajiv Kumar
am 12 Mär. 2017
if I have B = {[1 0 0 4],[0 0 0 0],[0 0 1 0],[0 0 2 3]} I want to remove zeros entries like this B = {[1 4],[],[1],[2 3]} How it is possible in cell vector
Akzeptierte Antwort
Weitere Antworten (4)
Ned Gulley
am 20 Jan. 2011
1 Kommentar
Jan
am 1 Feb. 2011
CELLFUN(@isempty) is remarkably slower than CELLFUN('isempty') as suggested by Matt Fig.
Michael Katz
am 20 Jan. 2011
I wanted to do:
strs = setdiff(strs,{''})
but turns out it reorders the output:
strs =
'four' 'one' 'three' 'two'
So, I wound up with this:
[~,ix] = setdiff(strs,{''})
strs = strs(sort(ix))
Bryan White
am 1 Feb. 2011
For variety:
cellstr(strvcat(strs))'
Kategorien
Mehr zu String Parsing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!