MATLAB: Mean of columns within matrix blocks of different dimensions

averagematrix blocksmean

I would like to create a matrix M where each column equals to the mean of the matrix blocks of the matrix A.
Case 1: The code below works well when I have subarrays of always 3 columns
B = reshape(A,2151,3,28);
M = squeeze(mean(B,2));
Case 2: But, now I need to group the columns of the matrix A either by 3 or 4.
B = mat2cell(A,2151,[3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 3 3 3]);
I created subarrays with mat2cell, but then I don't know how to average each blocks. The squeeze function doesn't work anymore.
Is it possible to produce a similar output than in Case 1 when I have subarrays of different dimensions ?
Thanks in advance for your help

Best Answer

Hi Fpet,
You can try the following:
B = mat2cell(A,2151,[3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 3 3 3]);
bAvgCell = arrayfun(@(x) mean(B{x},2),1:length(B),'UniformOutput',false);
M = cell2mat(bAvgCell);
Hope this helps.
Regards,
Sriram