How to average the columns within this cell array?
Show older comments
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
More Answers (0)
Categories
Find more on Matrices and Arrays 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!