How do I remove the empty cells from a vector of cells?
31 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Rajiv Kumar
2017 年 3 月 12 日
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
採用された回答
Hy
2011 年 1 月 20 日
The built-in function strcmp can compare a character array to a cell array of strings. Matching cells may be removed by setting them equal to the empty array.
strs(strcmp('',strs)) = [];
0 件のコメント
その他の回答 (4 件)
Matt Fig
2011 年 1 月 20 日
Probably the fastest approach:
strs = strs(~cellfun('isempty',strs)) % Call Built-in string
2 件のコメント
Ned Gulley
2011 年 1 月 20 日
1 件のコメント
Jan
2011 年 2 月 1 日
CELLFUN(@isempty) is remarkably slower than CELLFUN('isempty') as suggested by Matt Fig.
Michael Katz
2011 年 1 月 20 日
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))
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!