how to do sum of element of multiple number in matlab?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Putri Basenda Tarigan
le 18 Nov 2020
Commenté : Putri Basenda Tarigan
le 18 Nov 2020
Hello everyone.
If I have
m=[3 3 4 3 2 4 3]
c=[3 4]
then I want to calculate the sum of m(c) at the same time
I tried
b=1:m(c)
b=sum(b)
but only give me one number. I want it to be 2 numbers. How to do this?
Thanks in advance
2 commentaires
Réponse acceptée
Plus de réponses (1)
Timo Dietz
le 18 Nov 2020
Modifié(e) : Timo Dietz
le 18 Nov 2020
Not sure whether I got your issue. But in case you want to summarize all members of m starting at index 1, up to each member of c (containing indices of m) individually, you can do this:
b=arrayfun(@(x) sum(m(1:x)), c)
2 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!