# How to find the mean after every n columns?

7 views (last 30 days)
B.kun on 4 Mar 2015
Commented: B.kun on 4 Mar 2015
Hi all
In order to find the mean column-wise, I know that we should code as follows:
Now I have a matrix of size 770 rows x 5760 columns. But I would like to find the mean after EVERY 4 columns and store them in a new matrix. So my final answer matrix will have 770 rows x 1440 columns. How can I do that? Any help is appreciated!
Thank you.

Sean de Wolski on 4 Mar 2015
This could be done with a simple for-loop or a slightly trickier reshape:
% Simple data
x = repmat(1:16,3,1)
nrows = 4; % rows to mean
meanby4 = squeeze(nanmean(reshape(x.',nrows,size(x,2)./nrows,[]))).'
B.kun on 4 Mar 2015
Thank you! This works perfect! :)

Giorgos Papakonstantinou on 4 Mar 2015
Edited: Giorgos Papakonstantinou on 4 Mar 2015
NewMatrix = mymatrix(:,1:4:end);
##### 2 CommentsShowHide 1 older comment
B.kun on 4 Mar 2015
Thank you. I tried this but it didn't give right results. (I guess it skipped my data in between every 1st and 4th column).