Taking average of 3 rows from a matrix

18 Ansichten (letzte 30 Tage)
Hyunjae Jeon
Hyunjae Jeon am 20 Mär. 2019
Kommentiert: Hyunjae Jeon am 20 Mär. 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.

Akzeptierte Antwort

madhan ravi
madhan ravi am 20 Mär. 2019
Simpler and faster than the other two:
[m,n]=size(A); % A is your matrix
Mean=reshape(mean(reshape(A,3,[])),[],n)
  1 Kommentar
Hyunjae Jeon
Hyunjae Jeon am 20 Mär. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 20 Mär. 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 am 20 Mär. 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).';

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by