# How to use cumsum function?

1 view (last 30 days)
Arif Hoq on 30 Sep 2022
Commented: Jan on 6 Oct 2022
not able to get the expected output.
if col5<200 then, col5+25
and the result will be replaced in next row of col5 untill col5 <200.
for example:
B(30,5)=194.87 so, it will be 194.87+25 = 219.87
B(31,5)=219.87 +25 = 244.87
B(32,5)=244.87+25 = 269.87
B(33,5)=269.87+0= 269.87
B=A.B;
B(:,5)=B(1,4)-cumsum(B(:,3));
CG=25;
for i=1:size(B,1)
if B(i,5)<200
B(i,6)=CG;
else
B(i,6)=0;
end
B(i,7)=B(i,6)+cumsum(B(i,5));
% B(i,5)=B(i,7);
end
Jan on 30 Sep 2022
Please avoid pseudo-syntax like "if col5<200 then, col5+25". Use Matlab syntax, because the readers can understand it.
cumsum of a single element replies the value of the element. A cumulative sum is meaningful for a vector only.

Jan on 6 Oct 2022
capacity = 300;
if mat(i) < 200
mat(i) = mat(i) + 25;
end
end
mat

Jan on 30 Sep 2022
CG = 25;
C = 0;
for i = 1:size(B, 1)
if B(i, 5) < 200
if C == 0
C = CG;
else
C = 0;
end
end
B(i, 5) = B(i, 5) + C;
end
or shorter:
CG = 25;
C = 0;
for i = 1:size(B, 1)
if B(i, 5) < 200
C = CG * (C == 0);
end
B(i, 5) = B(i, 5) + C;
end
Jan on 6 Oct 2022
@Arif Hoq: I've posted it as separate answer.

### Categories

Find more on Time Series Events in Help Center and File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!