Extract runs of values from cell array

4 visualizaciones (últimos 30 días)
Alexander Collins
Alexander Collins el 17 de Jun. de 2021
Comentada: Alexander Collins el 17 de Jun. de 2021
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}

Respuesta aceptada

KSSV
KSSV el 17 de Jun. de 2021
iwant = C(~cellfun('isempty',C))
  3 comentarios
KSSV
KSSV el 17 de Jun. de 2021
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)
out{1} = 107 out{2} = 60 79 98 117
You may pick the cell which has more than one element.
Alexander Collins
Alexander Collins el 17 de Jun. de 2021
Thanks!
Must have had the wrong search terms for finding that original post.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by