create a matrix with elements as mean values of another matrix

1 回表示 (過去 30 日間)
pavlos
pavlos 2014 年 2 月 12 日
回答済み: Jos (10584) 2014 年 2 月 12 日
Hello,
Please help me with the following.
Consider a 100x10 matrix, called A.
How can I get a 100x10 matrix, called B, where each row is the mean of preseding rows of A?
For matrix B,
row1 = row1 of A
row2 = mean (row1,row2) of A
...
row100= mean(row1,row2,...,row100) of A
Thank you very much.
Best,
Pavlos

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 12 日
編集済み: Azzi Abdelmalek 2014 年 2 月 12 日
A=[1 2 3;4 5 6;7 8 9] % Example
[n,m]=size(A);
B=cumsum(A)./repmat((1:n)',1,m)
%or
A=[1 2 3;4 5 6;7 8 9]
B=bsxfun(@rdivide,cumsum(A),(1:size(A,1))')

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2014 年 2 月 12 日
Just to show the many roads to Rome in MatLab's world:
A = ceil(10*rand(10,4))
B = cell2mat(arrayfun(@(x) sum(A(1:x,:),1)/x, 1:size(A,1), 'un', 0).')

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by