Vector sum by index

11 visualizaciones (últimos 30 días)
Konstantin König
Konstantin König el 17 de Jul. de 2020
Respondida: KSSV el 17 de Jul. de 2020
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!

Respuestas (2)

madhan ravi
madhan ravi el 17 de Jul. de 2020
cumsum()

KSSV
KSSV el 17 de Jul. de 2020
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.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by