How to average the columns within this cell array?

43 次查看(过去 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!

采纳的回答

Piyush Kumar
Piyush Kumar 2024-7-31,8:56
"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.
  1 个评论
lil brain
lil brain 2024-7-31,9:23
Ah yes that is correct. I noticed that I didnt convert the tables into doubles prior. Thanks!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by