How to average columns of matrix without loop?

1 回表示 (過去 30 日間)
Csanad Levente Balogh
Csanad Levente Balogh 2021 年 1 月 6 日
回答済み: Vladimir Sovkov 2021 年 1 月 6 日
Hi! I have a rather big (about 1 000 000 X 10 000) matrix. I want to averaging n column blockes. For example:
n = 3;
A = [1 2 3 4 5 6 7 8 9;10 4 1 2 6 4 1 -4 0];
The result shuold be:
res =
2 5 8
5 4 1
I know it could be done with a for loop using mean, but in my code that would be inside of other for loops, which makes a huge runtime. What I basically need is a mooving averege without overleap, but I couldn't find such matlab function. I wonder, if there is a loop free solution for this.

採用された回答

Vladimir Sovkov
Vladimir Sovkov 2021 年 1 月 6 日
reshape(mean(reshape(A',n,[]))',size(A,2)/n,[])'

その他の回答 (1 件)

KSSV
KSSV 2021 年 1 月 6 日
There are functions possible to do the said. Read about movemean, blockproc, reshape.

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by