Hello
I want the sum of a vector by index, means I want to get the sum of all indicies until the current index plus the current index?
X = [1 , 4, 7, 10]
Output:
X_output = [1, 5, 12, 22 ]
Index1 = 1
Index2 = 1 + 4 = 5
Index3 = 1 + 4 + 7 = 12
Index4 = 1 + 4 + 7 + 10 = 22
Thanks!

回答 (2 件)

madhan ravi
madhan ravi 2020 年 7 月 17 日

0 投票

cumsum()
KSSV
KSSV 2020 年 7 月 17 日

0 投票

Using loop:
x = [1 4 7 10] ;
thesum = zeros(size(x)) ;
thesum(1) = x(1) ;
for i = 2:length(x)
thesum(i) = sum(x(1:i)) ;
end
For inbuilt function read about cumsum.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

タグ

質問済み:

2020 年 7 月 17 日

回答済み:

2020 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by