If the number of rows of M was an integer multiple of N then this would be easy to achieve by simply reshaping the matrix M. Because your data does not fit this we have to take into account that there may be from 1 to N last rows to be "collapsed" together. This can be achieved using mat2cell to split the matrix into groups, and then performing the mean inside a cellfun call:
N = 3;
M = [...
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 5];
row = N*ones(1,ceil(size(M,1)/N));
row(end) = mod(size(M,1)-1,N)+1;
col = size(M,2);
C = mat2cell(M,row,col);
out = cell2mat(cellfun(@(m)mean(m,1),C,'UniformOutput',false));
and the output in the command window:
>> out
out =
64.667 86.667 32 14 17.667 64.667 61.667 57 64 42.667
62.667 68 40 35.333 34 54.333 59.667 56.667 52 42.333
37.333 7.6667 63 93.333 88.667 37.333 32.667 38 43.333 63.667
11 18 100 77 84 36 43 50 27 5
Best Answer