Simple if/else logic in MATLAB
이전 댓글 표시
Hi there, I've been working on a piece of code for calculating voltage and electric field inside coaxial cables of different sizes.
While doing my computation, I needed to do an if/else statement and was boggled as to why these two versions of the statement did not work interchangeably. For reference, both these statements are executed in a nested for loop (i, j are declared - there is no syntax error - just a logic one).
Option 1:
if ((i < innerstartx) || (i > innerendx) || (j < innerstarty) || (j > innerendy))
V(i,j) = (V(i+1,j) + V(i-1,j) + V(i,j+1) + V(1,j-1))/4;
else
end
Option 2:
if (i>=innerstartx && i<=innerendx && j>=innerstarty && j<=innerendy)
else
V(i,j)=0.25*(V(i+1,j)+V(i-1,j)+V(i,j+1)+V(i,j-1));
end
Currently, option 2 works, but option 1 doesn't. I checked over the logic a few times and I am pretty sure it is sound (simply De Morgan's inversion).
I was just curious though, as I'd like to be able to avoid this sort of problem should it arise in the future.
Thanks for your help.
sas
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!