How to average the columns within this cell array?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi, I have a deeply nested cell array of cells which contains a lot of doubles (C_512_eye_numeric). I am aiming to average the columns of the doubles column-wise. I cant seem to achieve this.
I have tried this code but it seems to only work on the first cell.
% Assuming C_512_eye_numeric is already defined in the workspace
% Initialize the new dataset C_512_avg
C_512_avg = cell(size(C_512_eye_numeric));
% Loop through each cell at the top level
for i = 1:numel(C_512_eye_numeric)
% Loop through each cell at the second level
for j = 1:numel(C_512_eye_numeric{i})
% Check if the cell contains numeric matrices
if isnumeric(C_512_eye_numeric{i}{j})
% Get the matrix from the cell
matrix = C_512_eye_numeric{i}{j};
% Average the columns column-wise
avg_matrix = mean(matrix, 1);
% Save the averaged matrix in the new dataset
C_512_avg{i}{j} = avg_matrix;
else
% If the cell does not contain numeric matrices, just copy it
C_512_avg{i}{j} = C_512_eye_numeric{i}{j};
end
end
end
% Display the result
disp('Averaged dataset:');
disp(C_512_avg);
Can someone help?
Thanks!
댓글 수: 0
채택된 답변
Piyush Kumar
2024년 7월 31일
"C_512_eye_numeric" is a 7X4 cell.
Inside that, each cell is a cell.
Inside those cells, some cells are 512 X 2 double and some are 512 X 16 table.
You code is calculating average for double, but for tables, it is just copying -
% If the cell does not contain numeric matrices, just copy it
C_512_avg{i}{j} = C_512_eye_numeric{i}{j};
I suggest you to go inside each table and calculate the average. Now, inside tables, some columns are 'Date', 'SystemTime' and 'RunTime' which you may want to ignore.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!