How can I sum row element to the end?

3 ビュー (過去 30 日間)
zeezo
zeezo 2018 年 3 月 1 日
回答済み: Walter Roberson 2018 年 3 月 1 日
I have this matrix "a" and it size is changeable and I need to sum all elements then I the sum from the second elements to the end than the third element to the end and so on
I tired this for loop but does not
a=[2; 3; 8; 2];
[row,column]=size(a);
for i=1:row
y=sum (a(i,1),end) ;
tt(i,1)=y;
end
tt
The code I want it to work if I change the matrix "a" size

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 3 月 1 日
sum(a)-[0;cumsum(a(1:end-1))]

カテゴリ

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