How to average the columns within this cell array?

4 views (last 30 days)
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!

Accepted Answer

Piyush Kumar
Piyush Kumar on 31 Jul 2024
"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 Comment
lil brain
lil brain on 31 Jul 2024
Ah yes that is correct. I noticed that I didnt convert the tables into doubles prior. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!