Search code examples
matlabmatrixmeancell-array

how to calculate mean across the elements of a cellarray in matlab?


I have a cellarray A of dimension 64x8 with elements in the following dimension,

520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double
520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double
520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double    520x1 double
...................

Now I need to take MEAN across the 8 rows in each column, so that i will get just one 520X1 cell per row.

so, after applying mean across rows, my output should be something like,

520x1 double
520x1 double
520x1 double
520x1 double
............

So, my output would be a 64x1 cell array transformed from 64x8.

i tried with doing this with the following command,

avgCell = {mean(cat(3,C{:}),3)}

But, it gives 1X1 cell array with just one cell of dimension 520X1.

Kindly correct me, and suggest me if there is any function to deal with this. and also let me know if i need a loop to do this?


Solution

  • % Create sample data...
    
    A = cell(64,8);
    
    for i = 1:64
        for j = 1:8
            A{i,j} = rand(520,1);
        end
    end
    
    % Calculate column-wise means...
    
    B = mean(cell2mat(A),2);
    
    % Reshape the result into a cell array...
    
    C = mat2cell(B,repmat(520,64,1),1);