Nested if statement won't execute
Afficher commentaires plus anciens
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 commentaire
Roger Stafford
le 5 Août 2013
You should check on 'flip' to make sure it is the vector of positive integers that you think 'j' should range over.
Réponse acceptée
Plus de réponses (1)
Danica
le 5 Août 2013
0 votes
1 commentaire
Sven
le 5 Août 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!