comparing a number against a range of numbers in IF-ELSE
Show older comments
stockposition = [1 1 5 2 3;
1 3 5 2 4;
3 1 4 2 5];
for i=2:3
for j=1:2
if stockposition(i,j) ~= stockposition(i-1,1:2)
alpha = 1
else
alpha = 0
end
end
end
works correctly but if I change the not equal to equal as shown below
for i=2:3
for j=1:2
if stockposition(i,j) == stockposition(i-1,1:2)
alpha = 1
else
alpha = 0
end
end
end
It does not work. It gives me alpha = 0 always. Anyone knows why?
Answers (1)
Image Analyst
on 16 Mar 2013
When you do this:
stockposition(i,j) ~= stockposition(i-1,1:2)
you have a single number on the left, and a pair of numbers on the right. The right side is a 1 by 2 array [stockposition(i-1,1), stockposition(i-1,2)]. What are you thinking? How do you expect this comparison to go? Can you clue me in on your thought process?
Categories
Find more on Loops and Conditional Statements 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!