for and if loop with two variables does not work

Hi,
I have an if and a for loop and I can't really figure out why it does not work. Can you tell my mistake? Basically I want to create the vector "Vektor2Batterie" with the conditions that I specified. The only reason I did not use "VektorBatterie" directly is because that also did not work, and I figured that might be the problem.
VektorBatterie = [A1 A8 A2 A9 A3 A10 A4 A11 A5 A12 A6 A13 A7 A14];
Vektor2Batterie = zeros(14,1)'
for i =1:14
for n = 2:2:14
if VektorBatterie(i) > 3040
Vektor2Batterie(i) = 3040;
if VektorBatterie(n) > VektorBatterie(n-1)
Vektor2Batterie(n) = VektorBatterie(n-1);
else
Vektor2Batterie(n) = VektorBatterie(i);
end
end
end
end

댓글 수: 3

Correction: the else specification is wrong, that can just be ignored. Here's the version of the loop I initially wanted to use, without Vektor2Batterie:
VektorBatterie = [A1 A8 A2 A9 A3 A10 A4 A11 A5 A12 A6 A13 A7 A14]; %Vektor mit abwechslend den Lade und Entladeintegralen pro Tag,
%sodass immer ein Wochentag nebeneinander liegt
%BSP: A1 ist das Integral der Ladefläche für Montag, A2 das Integral der Entladefläche
for i =1:2:14
for n = 2:2:14
if VektorBatterie(i) > 3040 %Begrenzt die Kapazität auf 3040 kWh
VektorBatterie(i) = 3040;
if VektorBatterie(n) > VektorBatterie(n-1); %Wenn der Vektor an der Stelle i
VektorBatterie(n) = VektorBatterie(n-1);
end
end
end
end
Rik
Rik 2022년 1월 7일
It looks like you want just 1 loop that will cap odd numbered positions to 3040 and ensures even numbered positions are smaller or equal to the previous element.
Can you confirm that is what you want?
Yes thats exactly what I want!

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

 채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 7일
VektorBatterie(1:2:end) = min(VektorBatterie(1:2:end), 3040);
VectorBattery(2:2:end) = min(VektorBatterie(2:2:end), VektorBatterie(1:2:end));

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020a

질문:

2022년 1월 7일

댓글:

2022년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by