comparing a number against a range of numbers in IF-ELSE

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?

답변 (1개)

Image Analyst
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?

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 3월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by