adding an if condition so the negative value would be set to zero

1 次查看(过去 30 天)
Hello everyone i have this code in which i am calculating the cumulative sum for each iterations, i want to add a condition to set the negative DOD to zero, can you help me with this part
for i=1:length(t)
DOD(i)=cumsum((T(i)*30)/(3600*14.6));
end
  1 个评论
Torsten
Torsten 2022-7-27
I don't think it makes sense to set the negative elements of DOD to zero.
Better you set the negative T-values to zero in advance:
T(T<0) = 0;
for i=1:length(t)
DOD(i)=cumsum((T(i)*30)/(3600*14.6));
end

请先登录,再进行评论。

回答(1 个)

Chunru
Chunru 2022-7-27
for i=1:length(t)
DOD(i)=cumsum((T(i)*30)/(3600*14.6));
end
DOD = max(DOD, 0)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by