Pergunta

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!

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top