Nested if statement won't execute

조회 수: 8 (최근 30일)
Danica
Danica 2013년 8월 5일
I am having problems with the following bit of code:
for j = flip
if TheData(j,2) == i
TheData(j,28) = TheData(j,28)*-1;
end
end
The code is nested in an if statement which itself is nested in a larger for loop. The other loops are working fine, but, for some reason, the final if statement doesn't execute. TheData(j,2) and i are both integers, and the comparison is true on at least two iterations which I've been able to prove using
find(TheData(j,2) == i)
If I comment out the if statement, the line in between (TheData(j,28)=...) executes so I know it's a problem w/ the if statement, but I can't figure out what. Any help would be appreciated. Thanks.
  댓글 수: 1
Roger Stafford
Roger Stafford 2013년 8월 5일
You should check on 'flip' to make sure it is the vector of positive integers that you think 'j' should range over.

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

채택된 답변

Sven
Sven 2013년 8월 5일
편집: Sven 2013년 8월 5일
Perhaps the value of i is never set, or at least never set in the scope of the loop that you are running. Perhaps you mean j instead?
Without being set by other code, the MATLAB value i is the square-root-of-minus-one, which might why your if statement never evaluates as true.
Did this fix it for you?
If not, what you need to do is:
  1. Set a breakpoint at your if-statement line
  2. Run the function or script that contains your code (it will stop at the breakpoint)
  3. Check the contents of TheData(j,2) and i.
  댓글 수: 3
Sven
Sven 2013년 8월 5일
편집: Sven 2013년 8월 5일
Ok, pause iteration at the point where TheData(j,2) is 3 (ie, when you expect the equality i==TheData(j,2) to be true), and then check the following at the command line:
i - TheData(j,2) % Is this *exactly* zero or just very very close?
class(i)
class(TheData(j,2)) % Are they the same class? Probably shouldn't matter
size(i) % Are they truly scalar inside the context of the loop?
size(TheData(j,2))
If you find stepping through loops with the debugger tedious, you can just place the above lines before your if-statement and run. I suspect now that the first options is most likely, but it will be interesting to see what you find.
Image Analyst
Image Analyst 2013년 8월 5일
i can't be a vector of integers, as you claim, or else this would throw an error:
if TheData(j,2) == i
Also, like Roger asked above, what is flip? It must be a vector of 1 or more integers.

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

추가 답변 (1개)

Danica
Danica 2013년 8월 5일
flip is an m x 1 vector of integers, and, to clarify, i is the counter for a larger loop and that counter has been set equal to a 1 x n vector of integers so that for each iteration of the loop, i is actually only a scalar.
After stepping through, I realized the issue. The size of TheData(j,2) w/in the loop was m x 1 instead of 1 x 1 because j wasn't properly stepping through flip. Transposing flip fixed this, though I don't understand why Matlab didn't iterate through a column vector like it does a row vector (an explanation here would be nice if anyone knows). Thank you for your help.
  댓글 수: 1
Sven
Sven 2013년 8월 5일
From the tips section of "doc for":
To iterate over the values of a single column vector, first transpose it to create a row vector.
I agree that it's not immediately intuitive. Basically, I think of it as:
for i = X
... will iterate over each of the columns of X. If X is a row vector, i will be scalar at each iteration. If X is not a row vector, i will be the contents of X(:,1), X(:,2), etc, for each iteration.

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

카테고리

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