0

If I have a column of dates and a corresponding column of volume data, like this:

31,3
31,2
31,1
31,5
07,2
07,3
07,4
07,2
07,3
07,5
07,3
07,1
07,1
07,2
07,3
30,5
06,4

I want to add up the data in the right hand column, for each date. If I use accumarray like this:

orgSumVinDay=accumarray(dayIdx,vv); 
k=orgSumVinDay==0;
SumVininDay=orgSumVinDay;
SumVinDay(k)=[]

It works; I get:

11
29
 5
 4

which is correct because on the 31st, there were 3+2+1+5=11, etc.

However, I want to get a column showing the cumulative addition within each day, so that it looks like:

 3
 5
 6
11 
 2
 4
 9
11
14
19
22
23
24
26
29
 5
 4

and I'm not sure how to achieve this. Thanks!

0

1 Answer 1

2

Cannot check it right now but I believe you should be able to do it with accumarray (..., [], @cumsum). The last parameter will replace the default function sum with cumsum.

Sign up to request clarification or add additional context in comments.

2 Comments

Strange, accumarray(dayStr,minuteVolumes, [],@cumsum) gives the error "The function 'cumsum' returned a non-scalar value." But if I use a different function, like @max for example, the results are correct.
This works! cell2mat(accumarray(dayStr,minuteVolumes,[],@(x){cumsum(x)})); Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.