MATLAB: How to effectively get the mean for every three consecutive outputs

consecutive outputsmean

Hello, I have an array that is (21,7). I want to get the mean of every three rows from start to finish. For example, my results would output my dependent variable as [0 0 0 1 1 1 2 2 2] while my results/independent variable would be [2 3 2 9 8 7 17 21 16]. I would like to get a result where I get [0 1 2] and with the means [2.333 8 18]. Anyone have a code that would work for all of the columns in my array. I'm novice at MATLAB, so an easy to understand code would really help. If easier or more useful, if I could do the mean of the results for every dependent variable that was the same, that would work too (so I can avoid sorting next time). Thanks!

Best Answer

It would be useful if you used proper formatting in your question so we can see actual rows and columns.
Assuming the array
data = [1 10 19 28
2 11 20 29
3 12 21 30
4 13 22 31
5 14 23 32
6 15 24 33
7 16 25 34
8 17 26 35
9 18 27 36]
The mean of each block of 3 consecutive rows can be obtained by reshaping the matrix into a 3D arrays with 3 rows:
data3d = reshape(data, 3, [], size(data, 2))
Notice that the columns are now in the 3rd dimension. We can swap the dimensions to make taking the mean easier, by moving 1st dimension into 3rd, 3rd back to 2nd and 1st to second:
data3d = permute(data3d, [2 3 1])
Taking the mean across the pages, you get your result:
datamean3 = mean(data3d, 3)
Or as a one liner:
datamean3 = mean(permute(reshape(data, 3, [], size(data, 2)), [2 3 1]), 3)