MATLAB: Calculate arithmetic average based on block numbers

averageMATLAB

Hi there,
This is a 2D matlab cell that I have (SEE ATTACHED IMAGE)
I want to average block numbers 1,2,3,4 and add into a new matrix (say A), and then average of 5,6,7,8 in 2nd row of matrix A, and then average of 9,10,11,12… in the 3rd row, etc.
Any help will be greatly appreciated.
Thanks, Akul

Best Answer

Don't use figures, attach .mat files or paste short sections of text for data...nothing anybody can do with a figure except look.
But, in your case it's easy enough using the fact that ML internal storage is column-major and the builtin functions ability to operate on arrays--
X=xlsread('yourfile.xls'); % get the data into an array
A=mean(reshape(X,4,[])); % average by column sets of four variables
A=reshape(X,[],size(X,2)); % rearrange means by original columns
The above averages the block numbers as well; you can either
  1. Choose to use that average, too,
  2. Select X(:,2:end) instead and ignore column (changing size to match), or
  3. Ignore or overwrite the block column after done.
I'll use option 3) here--
A(:,1)=(1:size(A,1)).'; % write the new number of average block