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
댓글 수: 0
채택된 답변
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
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
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!