Saving results of a for loop in a cell array
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi. I have a cell array (DataFrame) that has 16 cell, each containing an array with different length (I have attached a picture). I am trying to loop through each individual array and remove the columns (from the second column to end) that contain values more than 20 (using cell fucntion), and save the result of each loop to a new cell array (NewCell).
I have wrote this for loop which works but it seems like it is only going through the first array.
How would I go about fixing that?
Any help is much appreciated.
NewCell = cell(1,numel(DataFrame)); %creat an empty cell for the results
for iAnimal = 1:size(DataFrame) %repeat for each cell in DataFrame (1:16)
currAnimal = DataFrame {iAnimal}; %access the array inside each cell in DataFrame
G = cellfun(@(x) isnumeric(x) && x>20 , currAnimal(:,2:end)); %find values bigger than 20 in second column to the last column
F=any(G) % index columns that have a value bigger than 20 (index 1)
F(1,1)=0 % set the index for the first column as 0 since we didn't consider it in G and we want to keep every array's first column
currAnimal(:,F)= []; % remove columns from the current array
for y = 1:size(DataFrame);
NewCell{y}=currAnimal
end
end
0 comentarios
Respuesta aceptada
VBBV
el 24 de Mzo. de 2023
Editada: VBBV
el 24 de Mzo. de 2023
NewCell = cell(1,numel(DataFrame)); %creat an empty cell for the results
for iAnimal = 1:numel(DataFrame) %repeat for each cell in DataFrame (1:16)
currAnimal = DataFrame {iAnimal}; %access the array inside each cell in DataFrame
G = cellfun(@(x) isnumeric(x) && x>20 , currAnimal(:,2:end)); %find values bigger than 20 in second column to the last column
F=any(G) % index columns that have a value bigger than 20 (index 1)
F(1,1)=0 % set the index for the first column as 0 since we didn't consider it in G and we want to keep every array's first column
currAnimal(:,F)= []; % remove columns from the current array
% processed result saved into NewCell array
NewCell{iAnimal}=currAnimal;
end
It seems you dont need the 2nd for loop. Just try with outer for loop and use its index to save the results after processing the data
2 comentarios
VBBV
el 24 de Mzo. de 2023
Editada: VBBV
el 24 de Mzo. de 2023
Instead of using
for iAnimal = 1:size(DataFrame) % size
use
for iAnimal = 1:numel(DataFrame) % numel
size will return a vector giving number of rows and columns in an array. You can use size when you want to iterate along specific dimension such as
for iAnimal = 1:size(DataFrame,1) % iterates along rows in array
or along columns as
for iAnimal = 1:size(DataFrame,2) % iterates along columns in array
Más respuestas (1)
Anton Kogios
el 23 de Mzo. de 2023
Editada: Anton Kogios
el 23 de Mzo. de 2023
It is hard to run your code without your data, but a potential source of error may be when you use 'for iAnimal = 1:size(DataFrame)'. size(DataFrame) will return [1,16], and consequently 1:[1:16] will return i = 1. Hence why you are only getting it to go through the first array.
I suggest changing it to
for iAnimal = 1:length(DataFrame)
and
for y = 1:length(DataFrame)
Ver también
Categorías
Más información sobre Cell Arrays 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!