How to average the columns within this cell array?

3 visualizaciones (últimos 30 días)
lil brain
lil brain el 31 de Jul. de 2024
Comentada: lil brain el 31 de Jul. de 2024
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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by