How to use cumsum function?

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 comentario

Jan
Jan el 30 de Sept. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 6 de Oct. de 2022

0 votos

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

Más respuestas (1)

Jan
Jan el 30 de Sept. de 2022

0 votos

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 comentarios

Arif Hoq
Arif Hoq el 4 de Oct. de 2022
Thank you very much @Jan. But, it does not meet my expectation. Let me make it clear.
B(i,5) is th battery capacity(kWh). CG(25 kWh) increases its capacity whenever battery capacity decreases to 200 kWh.
B(49,5)=133.049; % (which is <200)
B(49,7)=133.049+25; % 133.049+25 = 158.0490
% Till now its fine. But in the next hour battary capacity starts from B(49,7). i.e.
B(50,5)= 158.0490; % as it's <200, then
B(50,7)=158.0490+25; % 158.0490+25 = 183.0490. as it's <200, then
B(51,5)= 183.0490;
B(51,7)= 183.0490+25; % 183.0490+25 = 208.0490.
Arif Hoq
Arif Hoq el 4 de Oct. de 2022
below one is my expected result.
Jan
Jan el 4 de Oct. de 2022
@Arif Hoq: You've spent a lot of time to create this image. Remember, that I do not have any idea about what you are doing. I see 3 columns. The first element of the first column is blank, the other elements of the first column contain values. Are they related to the problem? What does "300-50" in the 2nd element of the 3rd column mean? Is this the first element of the 2nd column minus the constant? Formerly you have mentioned, that 25 is added. So what does this row mean:
50 250 arrow 300-50
I still do not understand, what you want to achieve.
The sentence "But in the next hour battary capacity starts from B(49,7)" is jard to understand also. "Hour"? "Battery capacity"? "kWh"? Too many information, which do not matter the question, and too few information about what you want to achieve.
Arif Hoq
Arif Hoq el 4 de Oct. de 2022
Editada: Arif Hoq el 4 de Oct. de 2022
If i consider the image problem:
load=[0;50;20;10;5;30;12;8;20;30;5];
capacity=300;
mat=capacity-cumsum(load)
mat = 11×1
300 250 230 220 215 185 173 165 145 115
What does "300-50" in the 2nd element of the 3rd column mean?
it's 1st element of 2nd column minus 2nd element of 1st column and keep doing this.
300-50 = 250;
250-20= 230;
230-10=220;
220-5=215;
% mat(6,1)=185 which is <200, therefore add 25.
% mat(6,1) would be 185+25 =210;
% mat(7,1) >> 210-12 =198 which is <200, therefore add 25.
% mat(7,1) would be 198;25 = 223;
expected result:
300
250
230
220
215
210
223
215
220
215
210
Arif Hoq
Arif Hoq el 5 de Oct. de 2022
Editada: Arif Hoq el 5 de Oct. de 2022
this might be a possible solution:
load=[0;50;20;10;5;30;12;8;20;30;5];
capacity=300;
mat=capacity(1,:)-cumsum(load);
for i=1:size(load)-1
if mat(i)<200
mat(i)=mat(i)+25;
mat(i+1)=mat(i)-cumsum(load(i+1));
else
mat(i+1)=mat(i)-cumsum(load(i+1));
end
end
mat
mat = 11×1
300 250 230 220 215 210 223 215 220 215
Jan
Jan el 5 de Oct. de 2022
Editada: Jan el 6 de Oct. de 2022
@Arif Hoq: cumsum is the cumulative sum over the elements of a vector. In your code the argument of cumsum is a scalar. Then you can omit the cumsum inside the loop. A simplified version of your code:
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
Avoid redefining important Matlab commands as "load" as variables.
Remember that size() replies a vector. Then for i = 1:size(x) is fragile. Use the unique and clear numel instead or sprcify the dimension: size(x,1).
Now you have removed the load values twice from the capacity. Is this wanted?
Arif Hoq
Arif Hoq el 6 de Oct. de 2022
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 el 6 de Oct. de 2022
@Arif Hoq: I've posted it as separate answer.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 30 de Sept. de 2022

Comentada:

Jan
el 6 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by