0

If I have a column of dates and a column of data, I use this code to find the cumulative sum of the data for each of the dates, as found in the third column:

orgcumulative=cell2mat(accumarray(day,data,[],@(x){cumsum(x)}));
k=orgcumulative==0;
CVD=orgCVD;
CVD(k)=[];

31,3,3
31,2,5
31,1,6
31,5,11
07,2,2
07,3,4
07,4,9
07,2,11
07,3,14
07,5,19
07,3,22
07,1,23
07,1,24
07,2,26
07,3,29
30,5,5
06,4,4

Now I want to divide each data point within a day by the sum of the data within that day. For example:

31,3,3,3/11
31,2,5,2/11
31,1,6,1/11
31,5,11,5/11    <-- 11 is the sum of data for the 31 date
07,2,2,2/29
07,3,4, %and so on...
07,4,9,
07,2,11,
07,3,14,
07,5,19,
07,3,22,
07,1,23,
07,1,24,
07,2,26,
07,3,29, <-- 29 is the sum of data for the 07 date
30,5,5,1
06,4,4,1

If I try:

fractions=cell2mat(accumarray(day,data,[],@(x){  data/sum(x)  }));

This will divide the entire second column by each of the sums. Is there a way to restrict this so the division only happens for the members of the second column within each day?

1 Answer 1

1

Would it not be easier to accumulate the total for each day using accumarray and then use the day array as an index when accessing the accumarray output, like so:

total = accumarray(day, data); % equivalent to accumarray(day, data, [], @sum)
fractions = data ./ total(day);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton. Should have thought of this!

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.