Summing a Matrix, row by row.

2 ビュー (過去 30 日間)
Ahsan Khan
Ahsan Khan 2013 年 10 月 15 日
回答済み: Andrei Bobrov 2013 年 10 月 15 日
Hello there, I want to get the sum of a matrix row by row. for example if a have a matrix:
A = [1,2,3;1,2,3;1,2,3]
then i want a to generat matrix B in which i will have:
B =[1,2,3;2,4,6;3,6,9]
i have an idea how to achieve this but can't get to work. It can be done by just reading and storing the first row as say A1 of matrix A and then using some sum(A1) and then placing this under a loop for all rows. any help would be appreciated.

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 15 日
A = [1,2,3;1,2,3;1,2,3]
B = cumsum(A)

その他の回答 (1 件)

ES
ES 2013 年 10 月 15 日
A = [1,2,3;1,2,3;1,2,3;1,2,3]
[m,n]=size(A)
sum=zeros(1,n)
sum_Var=zeros(1,n)
for loop1=1:m
sum_Var=sum_Var+A(loop1,:);
B(loop1,:)=sum_Var;
end

カテゴリ

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