Saving results of a for loop in a cell array
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
VBBV
am 24 Mär. 2023
Bearbeitet: VBBV
am 24 Mär. 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 Kommentare
VBBV
am 24 Mär. 2023
Bearbeitet: VBBV
am 24 Mär. 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
Weitere Antworten (1)
Anton Kogios
am 23 Mär. 2023
Bearbeitet: Anton Kogios
am 23 Mär. 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)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!