Search code examples
matlabloopscell-array

Calculate the average of nested cells


Suppose that we have this cell array:

data = {{[1,2,3],[3,6,3]},{[6,8,2],[1,1,1]},{[4,8,3],[1,2,3]},{[5,1,3],[1,0,2]}};

we have four main cells and in every cell we have two cells. I want average between four main cells like this:

Average 1 between:
1   2   3
6   8   2
4   8   3
5   1   3
---------
4   4.75   2.75

Average 2 between:
3   6   3
1   1   1
1   2   3
1   0   2
---------
1.5  2.25  2.25

Finally, we should gather these two results in a cell array. What is the fastest way to do this, with minimum number of loops?


Solution

  • Follow Adriaan's advise, but:

    x = reshape([data{:}],2,[])';
    means = reshape(mean(cell2mat(x)),[],2)'    
    means =    
        4.0000    4.7500    2.7500
        1.5000    2.2500    2.2500