remove all strings from nested cell array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Davindra Usov
el 22 de Jun. de 2022
Comentada: Davindra Usov
el 7 de Jul. de 2022
Hi,
I'm trying to remove all string/chars from a 4x10 cell array where each cell in the array contains a 40x1 column vector (so as you can see, it's nested). Most commands I usually use such as cell2mat don't work on this because the cell array is nested.
Or, if preferred, is there a way to read in all the elements of a certain column in one of the cells in a cell array apart from the elements containing strings? e.g:
col=rand(4,10) %matrix containing column indices
for i=1:4
for j=1:10
new_cell_arrray{i,j}=g{i,1}(:,col(i,j)); %g is another (existing) 4x1 cell array and new_cell_array is 4x10.
end
end
Thank you
1 comentario
Walter Roberson
el 22 de Jun. de 2022
If you "remove" an entry from a 2D array, the result cannot be a 2D array any more.
Respuesta aceptada
Walter Roberson
el 22 de Jun. de 2022
C = num2cell(randi(9,4,10));
C(randperm(numel(C), 4)) = {"string"};
C(randperm(numel(C), 4)) = {'chars'};
C
for K = 1 : size(C,2)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C(:,K));
subset{K} = C(mask, K);
end
subset
subset{1}, subset{2}
5 comentarios
Walter Roberson
el 24 de Jun. de 2022
"In each cell, I have 4 strings and 36 numbers."
Okay, I will generate 4 strings and 36 numbers per cell.
for J = 1 : 4
for K = 1 : 10
thiscell = num2cell(randi(9, 40, 1));
rp = randperm(40,4);
thiscell(rp) = {"string"};
C{J,K} = thiscell;
end
end
whos C
C{1}
subset = cell(size(C));
for J = 1 : numel(C)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C{J});
subset{J} = C{J}(mask);
end
whos subset
subset{1}
and see that after the filtering, there are 36 entries left. All of the string() objects and character vectors have been removed.
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!