Add elements of a vector

7 次查看(过去 30 天)
Sofia Melo Vasconcellos
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!

回答(2 个)

dpb
dpb 2017-3-6
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 2017-3-7
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);

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by