Clear Filters
Clear Filters

Add elements of a vector

35 views (last 30 days)
Sofia Melo Vasconcellos
Sofia Melo Vasconcellos on 6 Mar 2017
Answered: Roger Stafford on 7 Mar 2017
Hello Assuming you have a list of N elements, how to add those elements 10 out of 10, and store the values of each sum in a new vector? I have the impression that using a control structure would help, but I can not see how Thank you!

Answers (2)

dpb
dpb on 6 Mar 2017
Not sure what you really mean here, but what about cumsum, maybe???
>> z=1:10;
>> cumsum(z)
ans =
1 3 6 10 15 21 28 36 45 55
>>
If that's the right answer to the wrong question, give an example input and expected output and how it was obtained.

Roger Stafford
Roger Stafford on 7 Mar 2017
Your phrase “add those elements 10 out of 10” is not very specific. I give the solutions to two possible interpretations. Let V be the vector of N elements.
1.) N is a multiple of 10 and you want to take the sum of successive blocks of 10.
Result = sum(reshape(V,10,[]),1);
2.) You want the sum of each successive subsequence V(1:10), V(2:11), V(3:12), V(4:13), ..., V(N-9:N).
Result = sum(hankel(V(1:10),V(10:N)),1);

Tags

Community Treasure Hunt

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

Start Hunting!