Add elements of a vector
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
dpb
le 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.
0 commentaires
Roger Stafford
le 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);
0 commentaires
Voir également
Catégories
En savoir plus sur Simulink Environment Customization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!