How to sum two rows in a matrix?

18 ビュー (過去 30 日間)
Moe
Moe 2014 年 4 月 26 日
コメント済み: Moe 2014 年 4 月 26 日
Hi everyone,
Suppose I have a matrix:
a = [4 2 1;1 9 6;4 22 4;12 15 1;1 6 8;5 2 6];
I want to summing every two rows together and write in matrix b:
Such as:
b= [23; 58; 28]
details of calculation:
23 = a (1,:) + a (2,:)
58 = a (3,:) + a (4,:)
28 = a (5,:) + a (6,:)
Can anyone help me please?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 26 日
編集済み: Azzi Abdelmalek 2014 年 4 月 26 日
a = [4 2 1;1 9 6;4 22 4;12 15 1;1 6 8;5 2 6]
b=sum(reshape(a',2*size(a,2),[]))
  1 件のコメント
Moe
Moe 2014 年 4 月 26 日
Thank you Azzi, just one transpose was needed in the last.

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

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2014 年 4 月 26 日
b = sum(reshape(a.',2*size(a,2),[]),1).'; % Assume an even no. of rows
  1 件のコメント
Moe
Moe 2014 年 4 月 26 日
Thanks a lot Roger!

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

カテゴリ

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