Nested if statement won't execute
显示 更早的评论
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
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.
采纳的回答
更多回答(1 个)
Danica
2013-8-5
0 个投票
1 个评论
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!