MATLAB: How to get average of multiple rows and subtract from one group of average to another

matlab function

Hello,
I have a big file with multiple rows and columns. I need to get the average of every 30 rows. for example from row 1 to 30, 31 to 60, 61 to 90 and so on until the end of the rows. Can anyone help me with this? any kinds of help will be really appriciated.

Best Answer

M = your matrix with 10039 rows and 64 columns
N = 30;
whole_blocks = floor(size(M,1)/N);
leftover = size(M,1) - whole_blocks * N;
last_whole = whole_blocks * N;
whole_mean = reshape( mean(reshape(M(1:last_whole, N, [])), 2), whole_blocks, [] );
extra_mean = mean(M(last_whole+1:end, :),1);
overall_mean = [whole_mean; extra_mean];