How to get Cumulative sum of elements based on negative sign
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Raghavendra Reddy P
le 12 Fév 2015
Commenté : Raghavendra Reddy P
le 23 Fév 2015
I have 2 vectors >> z=1:9; >> l=[1 2 -3 4 -5 6 7-8 9 10] starting from last element of l i.e 10 9....1 check for 1st negative element i.e. l=-8 if l=-8 add z8+z9 check for next negative element i.e l=-5 if l=-5 add z5+z6+z7 (elements between two negative numbers -5 and -8) check for next negative element i.e l=-3 add z3+z4 and add z1+z2 finally i should get new vector w=[z1+z2, z3+z4,z5+z6+z7,z8+z9]
0 commentaires
Réponse acceptée
Guillaume
le 12 Fév 2015
Your array l has one more element than z. why? Anyway,
z = 1:9;
l = [1 2 -3 4 -5 6 7 -8 9];
negposition = find(l < 0);
w = arrayfun(@(s, e) sum(z(s:e)), [1 negposition], [negposition-1 numel(z)])
9 commentaires
Guillaume
le 19 Fév 2015
I don't see how you differentiate between your laterals and feeders in L.
In any case, I would suggest you start a new question. You'll get more people looking at it as when a question has an accepted answer, people don't look at them anymore.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Variables 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!