Nested if statement won't execute
Mostrar comentarios más antiguos
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
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.
Respuesta aceptada
Más respuestas (1)
Danica
el 5 de Ag. de 2013
0 votos
1 comentario
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.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!