Taking average of 3 rows from a matrix

16 views (last 30 days)
I have a data that has 10 columns and 369 rows.
I would like to get average of every 3 rows so that the output will be 10 columns 123 rows.
So that I will be getting average of first three rows in the first row in the output spreadsheet.
Thank you in advance.

Accepted Answer

madhan ravi
madhan ravi on 20 Mar 2019
Simpler and faster than the other two:
[m,n]=size(A); % A is your matrix
Mean=reshape(mean(reshape(A,3,[])),[],n)
  1 Comment
Hyunjae Jeon
Hyunjae Jeon on 20 Mar 2019
I was checking google and Mathworks and found the reshape function. However, it was hard to apply it to my case. However, your comment on "n being number of column" clarified quite a lot.
Thank you

Sign in to comment.

More Answers (2)

KSSV
KSSV on 20 Mar 2019
A = rand(369,10) ;
idx = repmat(3,1,369/3) ;
% arrange data
B = mat2cell(A,idx) ;
% get mean
M = cellfun(@mean,B,'un',0) ;
M = cell2mat(M)

madhan ravi
madhan ravi on 20 Mar 2019
Using mat2cell() and cellfun() is slower than an explicit loop follow this instead:
[m,n]=size(A); % A is your matrix
U1=reshape(A.',n,3,m/3);
Uu=mean(permute(U1,[2 1 3]));
Mean=squeeze(Uu).';

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!