필터 지우기
필터 지우기

How to use cumsum function?

조회 수: 2 (최근 30일)
Arif Hoq
Arif Hoq 2022년 9월 30일
댓글: Jan 2022년 10월 6일
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
A=load("B.mat");
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
  댓글 수: 1
Jan
Jan 2022년 9월 30일
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
Jan 2022년 10월 6일
loadValue = [0;50;20;10;5;30;12;8;20;30;5];
capacity = 300;
mat = capacity(1,:) - cumsum(loadValue);
for i = 1:numel(loadValue)-1
if mat(i) < 200
mat(i) = mat(i) + 25;
end
mat(i+1) = mat(i) - loadValue(i+1);
end
mat

추가 답변 (1개)

Jan
Jan 2022년 9월 30일
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
  댓글 수: 8
Arif Hoq
Arif Hoq 2022년 10월 6일
Thank you very much @Jan. you are right, i can omit the cumsum inside the loop. will try to follow your tips on "Load" & "numel / size". that's my expected answer. could you please make it as an answer ?
Jan
Jan 2022년 10월 6일
@Arif Hoq: I've posted it as separate answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by