Saving results of a for loop in a cell array

13 次查看(过去 30 天)
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

采纳的回答

VBBV
VBBV 2023-3-24
编辑:VBBV 2023-3-24
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 个评论
VBBV
VBBV 2023-3-24
编辑:VBBV 2023-3-24
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

请先登录,再进行评论。

更多回答(1 个)

Anton Kogios
Anton Kogios 2023-3-23
编辑:Anton Kogios 2023-3-23
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)
  1 个评论
Noush
Noush 2023-3-24
Thank you Anton. I was not quite sure the difference between using size vs length. that is very helpul.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by