Extract runs of values from cell array
2 次查看(过去 30 天)
显示 更早的评论
I have a cell array containing many empty cells, the occasional isolated value surrounded by empty cells, and a few runs of values in consecutive cells. e.g.:
C = {[] [] 107 [] [] [] [] [] [] [] [] [] 60 79 98 117 [] []}
Is there a way to pull out only those cells that form part of a run of values?
i.e.
B = {60 79 98 117}
0 个评论
采纳的回答
KSSV
2021-6-17
iwant = C(~cellfun('isempty',C))
3 个评论
KSSV
2021-6-17
C = {[] [] 107 [] [] [] [] [] [] [] [] [] 60 79 98 117 [] []} ;
idx = cellfun(@isempty,C) ;
C(idx) = {NaN} ;
C = cell2mat(C) ;
ii = zeros(size(C));
jj = C > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',C(jj)',[],@(x){x'});
celldisp(out)
Reference: https://in.mathworks.com/matlabcentral/answers/104614-grouping-continuous-nonzero-in-a-row-vector
You may pick the cell which has more than one element.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!