This question is related to http://stackoverflow.com/questions/3101893/how-can-i-perform-this-cumulative-sum-in-matlab.
Is it possible to use TWO conditions to perform cumsum? or just one condition?
EDITED:
data = [1 22 20;... %# Initial data
1 22 22;...
1 22 20;...
1 44 11;...
0 44 12;...
1 33 99;...
1 33 20;...
1 33 50];
I want to find cumulative sum which fulfilled 2 conditions:
% 1) current row in column 1 = 1 && previous row in column 1==1;
% 2) current row in column 2 = previous row in column 2
data(:,4) = cumsum(data(:,3)); % Add a 4th column containing
% the cumulative sum of column 3
index = diff([0;data(:,1)])> 0 && diff([0;data(:,2); 0])~= 0;
offset = cumsum(index.*(data(:,4)-data(:,3)));
data(:,4) = data(:,4)-offset;
index = (data(:,1) == 0);
data(index,4) = data(index,3)
Intended output:
data = [1 22 20 20 >> 20 + 0
1 22 20 40 >> 20 + 20
1 44 11 84 >> 11 + 40
0 44 12 12 >> 12 + 0
1 33 99 99 >> 99 + 0
1 33 20 119 >> 20 + 99
0 33 50 50 >> 50 + 0
EDITED: With the code below, I got the wrong output.
index = diff([0;data(:,1)])> 0 & diff([0;data(:,2)])~=0
1 22 20 20
1 22 22 42
1 22 20 62
1 44 11 73 %this supposed to be 11 not 73 ..
0 44 12 12
1 33 99 99
1 33 20 119
1 33 50 169