Taking average of 3 rows from a matrix
16 views (last 30 days)
Show older comments
Hyunjae Jeon
on 20 Mar 2019
Commented: Hyunjae Jeon
on 20 Mar 2019
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.
1 Comment
Accepted Answer
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)
More Answers (2)
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
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).';
0 Comments
See Also
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!