If statement indexing in a for loop

I have made a for loop which models energy going being stored every hour. Three times a day energy is removed from storage. so far so good. I want to be able to check if the energy stored reaches a minimum value in the hour before energy is removed and if it does not then to add suffcient energy to reach the minimum value and record how much has been added. I'm trying to use an IF statement to interact with three columns in my matrix but am unable to make it work, it seems within the for loop my matrix becomes a vector? How can I isolate my three columns?

댓글 수: 5

Cris LaPierre
Cris LaPierre 2021년 3월 15일
Share your code.
Calum Mackenzie
Calum Mackenzie 2021년 3월 15일
restored = transpose(reshape(stored,24,92));
reavlesuse = transpose(reshape(avlesuse,24,92));
topupstored = zeros(92,24);
for i = 2:numel(restored);
if topupstored(i) % is in columb 7,12 or 16
% if topupstored(i) < min then find min - topupstored(i) and add to topupstored(i)
% else
% else
topupstored(i) = restored(i-1) + reavlesuse(i);
end
topupstored(i) = max(min(restored(i), ub), lb);
end
Calum Mackenzie
Calum Mackenzie 2021년 3월 15일
Sorry not sure how to upload vairables, I've added the comments to try and express what I want to achieve.
Calum Mackenzie
Calum Mackenzie 2021년 3월 15일
Hopefully this better explains what I'm trying to do
Cris LaPierre
Cris LaPierre 2021년 3월 15일
Save your variables to a mat file and attache the mat file using the paperclip icon.

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 3월 15일

0 개 추천

I would do this without a for loop.
  • Compute min-columns 7,12,16
  • Set any values <0 to 0 (you don't have to add to these rows to reach the minimum)
  • Add the result to the previous hour (column?)

댓글 수: 2

Calum Mackenzie
Calum Mackenzie 2021년 3월 16일
I don't think this will work since the energy stored is dependant on the previous hour. So if I add 'top up' energy the first time there is a shortfall, that extra energy will be carried forwards to subsequent hours and likley reduce and may eliminate subsiquent shortfalls. Thats why i think it needs to be done sequentially in a loop?
Cris LaPierre
Cris LaPierre 2021년 3월 16일
Ok, so add it to columns (7:end) the first time, etc.
You are going to understand the problem better than any of us. We can only operate on the information you've shared.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 3월 15일

댓글:

2021년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by