Search code examples
matlabcellmean

Mean of rows and columns of matrices in a cell


I have say n a x b matrices and I want to generate a new matrix of dimension a x b which is the mean of all n a x b matrices, i.e the first element of this new matrix is the mean of all first elements in each n a x b matrices and so on. Is there a way to compute this average matrix from a group of matrices in MATLAB? I had tried to do this by creating a cell but couldn't figure out how to take mean of each element of these matrices. I would appreciate any ideas or suggestions.


Solution

  • First, put your n matrix in a single axbxn matrix

    M = cat(3, mat1, mat2, mat3, ...);
    

    Or, if you work with a cell array,

    M = cat(3, cellOfMats{:})
    

    Then just use mean along the third dimension

    meanmat = mean(M,3)