Extract runs of values from cell array
조회 수: 4 (최근 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!