필터 지우기
필터 지우기

Adding a counter to a loop

조회 수: 15 (최근 30일)
Mohammad Dabbagh
Mohammad Dabbagh 2021년 6월 23일
댓글: Mathieu NOE 2021년 6월 24일
Hi there,
I was wondering if anyone could do me a favor and let me know why my added counters blow does not work!
Thak you so much in advance.
for time = 1 : 8760
CountWintW=0;
CountWintE=0;
CountWintS=0;
CountWintN=0;
for WinNum=1:4
if AbsRad(time,WinNum) >= 0.0001
NewSHGC(WinNum) = shgc.open;
NewWinR(WinNum) = 1/U_factor.open;
else
NewSHGC(WinNum) = shgc.close;
NewWinR(WinNum) = 1/U_factor.close;
if WinNum ~=1
CountWintW = CountWintW+1;
elseif WinNum ~=2
CountWintE = CountWintE+1;
elseif WinNum ~=3
CountWintS = CountWintS+1;
elseif WinNum ~=4
CountWintN = CountWintN+1;
end
end
end
end
  댓글 수: 3
Rik
Rik 2021년 6월 23일
You clearly didn't try running your code line by line with the debugger.
If you had done so, you would have spotted 2 issues:
  1. every time step you're resetting the counters
  2. because you're using ~=, the third option will never be reached (if WinNum is 1 the second counter will increment, if WinNum is any value not equal to 1 the first counter will increment)
It also looks like it would make sense to use a vector and use WinNum as the index:
CountWin=zeros(1,4);
for time = 1 : 8760
[...]
CountWin(WinNum)=CountWin(WinNum)+1;
Mohammad Dabbagh
Mohammad Dabbagh 2021년 6월 24일
Thank you so much, Rik!

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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 6월 23일
hello
IMHO, the initialisation of the counters should be done BEFORE the first for loop
CountWintW=0;
CountWintE=0;
CountWintS=0;
CountWintN=0;
for time = 1 : 8760
for WinNum=1:4
if AbsRad(time,WinNum) >= 0.0001
NewSHGC(WinNum) = shgc.open;
NewWinR(WinNum) = 1/U_factor.open;
else
NewSHGC(WinNum) = shgc.close;
NewWinR(WinNum) = 1/U_factor.close;
if WinNum ~=1
CountWintW = CountWintW+1;
elseif WinNum ~=2
CountWintE = CountWintE+1;
elseif WinNum ~=3
CountWintS = CountWintS+1;
elseif WinNum ~=4
CountWintN = CountWintN+1;
end
end
end
end
  댓글 수: 2
Mohammad Dabbagh
Mohammad Dabbagh 2021년 6월 24일
Thank you so much, Mathieu!
Mathieu NOE
Mathieu NOE 2021년 6월 24일
you're welcome !

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

추가 답변 (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