Removing empty cells from cell array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joel Schelander
el 26 de Mzo. de 2021
Comentada: Joel Schelander
el 26 de Mzo. de 2021
G is a cell array:
G={16x16 cell 16x16 cell 16x16 cell 16x16 cell 16x16 cell 16x16 cell}
Each cell look like this: G{1}=[1 [] 3 ; 4 [] 7]; (but 16x16)
I want to remove these empty cells []. I have tried
index = cellfun(@isempty, G) == 0;
Gnew = G(index)
Without success. How can I solve this?
2 comentarios
Stephen23
el 26 de Mzo. de 2021
"Each cell look like this: G{1}=[1 [] 3 ; 4 [] 7]; "
I doubt that, as numeric arrays must be rectangular and cannot contain "holes":
[1,[],3;4,[],7]
Please upload some sample data in a mat file by clicking on the paperclip button.
Respuesta aceptada
Stephen23
el 26 de Mzo. de 2021
Editada: Stephen23
el 26 de Mzo. de 2021
Most likely converting the nested cell array to numeric arrays is going to make processing your data easier:
S = load('G.mat');
GUD = S.GUD
GUD{1}
Convert:
for k = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{k});
tmp = zeros(size(GUD{k})); % or perhaps NAN.
tmp(idx) = [GUD{k}{idx}];
GUD{k} = tmp;
end
Checking:
GUD
GUD{1}
If you really want to keep the (very inefficient and difficult to work with) nested cell arrays containing scalar numerics, then something like this:
for k = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{k});
GUD{k}(idx) = {0};
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!