Add elements of a vector
35 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (2)
dpb
am 6 Mär. 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.
0 Kommentare
Roger Stafford
am 7 Mär. 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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!