MATLAB: Average between files with cell array of doubles

averagecell arrays

Hi matlab people!
I have a cell array 'data' of (40,50) cells, each of cell is a matrix [70 x 100]
I need to get 1,50 cells of [70 x 100] averaging for 40 rows in each column of cells
So i try to run the loop:
for i = 1:50
for j=1:1:70
for k=1:100
for h=1:40
a = []
a(h) = data{h,i}(j,k)
end
avg_a=mean(a)
avg_cells{1,i}(j,k) = avg_a
end
end
end
What do i do wrong? Or maybe there is a simplier way to average between cell-arrays?

Best Answer

I'm assuming you're want to average element-wise across the rows of data. If this doesn't suit your needs, please let me know what's different from your data.
% CREATE DEMO DATA
% I have a cell array 'data' of (40,50) cells,
% each of cell is a matrix [70 x 100]
data = reshape(arrayfun(@(i){randi(20,70,100)},1:40*50),40,50);
% COMPUTE ELEMENT-WISE MEAN ACROSS ROWS
% I need to get 1,50 cells of [70 x 100]
% averaging for 40 rows in each column of cells
avg = arrayfun(@(i){mean(cat(3,data{:,i}),3)},1:size(data,2));