Why won't my if statement execute in the for loop?
6 views (last 30 days)
Show older comments
MATmagician
on 28 Apr 2016
Commented: Image Analyst
on 29 Apr 2016
I have the following code which sets variables used to a for loop:
%Find the entry of P that lambda is most sensitive to, i.e. the largest
%value in the elasticity matrix. M finds the largest entries in the columns of P and puts them into
%a row vector. I returns the row index of the elements of M found in E:
[M,I] = max(E);
disp(M)
disp(I)
index=find(M==max(M)); %Find index of largest element in M. This corresponds to the column of E this element was found in
disp('most sensitive found in column')
disp(index)
mostSensitive=E(I(index),index); %Set mostSensitive to the largest value found in M
and I wish to execute the if statement in the following for loop:
%Now create a vector which counts how many times mostSensitive appears in
%the elasticity matrix:
numAppearances = 0;
rowIndices=[];
for k=1:size(M) %Traverse vector of maximum values of columns of E
disp('We are at index')
disp(k)
disp('We are at element')
disp(M(k))
disp(mostSensitive)
if M(k)==mostSensitive %If the current element in the vector of the largest elements of S is equivalent to the highest one found,
disp('In if statement suite')
numAppearances=numAppearances+1;
rowIndices(k)=I(k);
end
end
When the interpreter reaches the if statement, it will not execute. I have taken the followng steps:
- MATLAB documentation has not informed me on why this may be happening.
- I have used error checks using disp(x) and have seen that the if condition is true
- I have also used the debugger
- I have looked at similar questions on MATLAB answers and generally googled solutions
0 Comments
Accepted Answer
Image Analyst
on 28 Apr 2016
If it does actually execute statements in the for loop, but the "if" condition is false, then I suspect you're comparing floating point numbers. And you'll have to read the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Look at what your disp statements put out into the command window. Does M(k) or mostSensitive have any fractional components? If so, then the FAQ definitely applies. What does this say:
whos M
whos mostSensitive
If either is a double, then you might use tolerances like the FAQ shows you, or else cast them to int32 if you can.
3 Comments
Image Analyst
on 29 Apr 2016
You're welcome. Granted, it's not obvious so don't feel bad. Thanks for Accepting.
More Answers (2)
Image Analyst
on 28 Apr 2016
My guess is that M is empty so that it never enters the for loop. Why can you not discover that it never steps into the for loop if you're using the debugger? Are you sure you know how to use it?
Walter Roberson
on 28 Apr 2016
You have
for k=1:size(M) %Traverse vector of maximum values of columns of E
that needs to use length(M) instead of size(M) . Remember that size(M) is a vector, and you cannot use a vector as a limit in a colon operator.
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!