comparing a number against a range of numbers in IF-ELSE
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Image Analyst
2013-3-16
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?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!