Create new smaller Matrix by summing every n-rows in old Matrix (without loops)

3 ビュー (過去 30 日間)
Tim
Tim 2013 年 12 月 17 日
コメント済み: Tim 2013 年 12 月 17 日
I've a Matrix like A =
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
I want this 9x3 Matrix to become a (9/n)x3 Matrix (n=3) where every third row is added up. The result would then be:
B =
12 12 12 %because 1+4+7
15 15 15 %because 2+5+8
18 18 18 %because 3+6+9
Is there a way e.g. to use sum() or so? I justed a loop over all rows but my actual Matrix contains a lot of data which is why that would take too long...
Thanks a lot!
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2013 年 12 月 17 日
it's homework?
Tim
Tim 2013 年 12 月 17 日
nope, more something like a project...There's this older programme that I've to make more efficient..It handels huge amount of data and only exists of while-and for-loops so far...But I don't have that much experience in this programming style of using vectors and matlab-functions..yet ;-) So thanks a lot (again)!

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 17 日
編集済み: Andrei Bobrov 2013 年 12 月 17 日
s1 = size(A);
s2 = [3 3]; % size of the matrix- result
ii = rem((1:s1(1))'-1,s2(1))+1;
[i1,i2] = ndgrid(ii,1:s1(2));
B = accumarray([i1(:),i2(:)],A(:));
or just:
B = sum(reshape(A',size(A,2),3,[]),3)';

その他の回答 (0 件)

カテゴリ

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