Sum of an array elements with condition on the sum value
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everybody,
I would like some help here.
So I have a vector (C) that has 10 random numbers.
C = [ 2 5 4 3 6 7 3 4 2 1]. I would like to cumulatively sum the elements with the condition of the sums not exceeding 12 i.e 2+5+4 = 11 (under 12) then move to the next 3+6 = 9 (under 12) .. ans so on. Then storing the cumulaitve sums in a differnt vector G. So the results will be like: G = [11 9 10 7].
Thank you
1 Kommentar
dpb
am 30 Nov. 2019
Bearbeitet: dpb
am 30 Nov. 2019
Probably just the straightforward loop is the simplest here...
You could use cumsum and locate the position via find(cumsum(C)<12,1,'last') and then repeat again with the subsequent portion of the original vector, but I suspect just the straightahead sum will be quicker to code and probably faster.
No really unique solution pops to mind....not to say there isn't one.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!