Averaging setions of a matix

1 回表示 (過去 30 日間)
AUWAL ABUBAKAR
AUWAL ABUBAKAR 2019 年 11 月 27 日
回答済み: Andrei Bobrov 2019 年 11 月 27 日
Good day,
Could someone please help me on how to average row one to nth row of a matrix and build another matrix with the out put.
For instance, for a matrix 'A' of 20 x 5, I would like to build another matrix of which its first row is an average of the first two rows of A, the 2nd row is the average of first three rows of A and the third row is the average of the 1st four rows of A, until the last row of matrix A.
I tried the loop shown below but I got an error that dimension of the right and left side are not the same.
for f=1:1:n
k=mean(B(1:n,:))
f
Avg(:,f)=k
end
Thanks in advance

採用された回答

dpb
dpb 2019 年 11 月 27 日
mnA=cell2mat(arrayfun(@(n)mean(A(1:n,:)),[2:size(A,1)].','uni',0));
  1 件のコメント
AUWAL ABUBAKAR
AUWAL ABUBAKAR 2019 年 11 月 27 日
This is awsome!
It works fine.
Thanks for your help

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2019 年 11 月 27 日
out = cumsum(A)./(1:size(A,1))';
mnA = out(2:end,:);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by