How to sum some columns of a matrix?

29 ビュー (過去 30 日間)
Alvaro
Alvaro 2017 年 6 月 1 日
編集済み: Chris Howard 2022 年 5 月 6 日
Hi,
I am trying to add values in a matrix that each is identified as I(x,y).
What I need to do is to sum some columns and put them into a new matrix. For example, I want to add I(x1,y1)+I(x1,y2)+I(x1,y3) and put it in first column first row of the new matrix. Then add I(x2,y1)+I(x2,y2)+I(x2,y3) and put this value on the first column second row of the new matrix while keeping the other columns of the matrix the same... and so on.
For example:
A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
% sum columns 2 and 3 in B
% B=[1 5 4; 5 13 8; 9 21 12; 13 29 16]
Can anyone help me with this please?
All the best Alvaro

採用された回答

the cyclist
the cyclist 2017 年 6 月 1 日
A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B = [A(:,1) sum(A(:,[2 3]),2) A(:,4)]
  2 件のコメント
Alvaro
Alvaro 2017 年 6 月 1 日
Thank you! I have lost a lot of time trying to solve something easy
Chris Howard
Chris Howard 2022 年 5 月 6 日
編集済み: Chris Howard 2022 年 5 月 6 日
Uddipan would be proud...

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 1 日
B = blockproc(padarray(A,[0,1],0),[1,2],@(x)sum(x.data,2))

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by