Nested if statement won't execute

7 visualizaciones (últimos 30 días)
Danica
Danica el 5 de Ag. de 2013
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 comentario
Roger Stafford
Roger Stafford el 5 de Ag. de 2013
You should check on 'flip' to make sure it is the vector of positive integers that you think 'j' should range over.

Iniciar sesión para comentar.

Respuesta aceptada

Sven
Sven el 5 de Ag. de 2013
Editada: Sven el 5 de Ag. de 2013
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 comentarios
Sven
Sven el 5 de Ag. de 2013
Editada: Sven el 5 de Ag. de 2013
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 el 5 de Ag. de 2013
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.

Iniciar sesión para comentar.

Más respuestas (1)

Danica
Danica el 5 de Ag. de 2013
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 comentario
Sven
Sven el 5 de Ag. de 2013
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by