if statement and "equal to" problem

Hello everybody. I have a simple "if statement with equal to" and I would like to set a list of value instead of just one. Maybe the example will clarify my problem:
for j=1:length(POS_S)
for i=1:length(POS_C)
if (POS_S(j) == 7,8,9) AND ((POS_C(i) == POS_S(j)+1) OR (POS_C(i) == POS_S(j)-1)) AND (POS_C(i) ~= 2,3,4);
....
end
I receive the following error:
Error: Unexpected MATLAB expression.
Anyone who can help me? Where I'm wrong? Is it possible put a list values after "==" and "~=" ?

답변 (2개)

Image Analyst
Image Analyst 2015년 9월 26일

0 개 추천

Use double ampersand, &&, instead of the word AND.

댓글 수: 2

Image Analyst
Image Analyst 2015년 9월 26일
and double vertical bar instead of the work "OR".
John D'Errico
John D'Errico 2015년 9월 26일
Funny. I completely missed the use of OR and AND in that line of code. Again, not valid MATLAB syntax.

댓글을 달려면 로그인하십시오.

John D'Errico
John D'Errico 2015년 9월 26일
편집: John D'Errico 2015년 9월 26일

0 개 추천

This is not valid MATLAB syntax:
(POS_S(j) == 7,8,9)
At least, it won't do what you obviously desire it to do. Making up syntax on your own rarely is successful.
You might consider using ismember instead. You could also have used three explicit tests, with an or between them. (Thus the | | operator.)

댓글 수: 1

Image Analyst
Image Analyst 2015년 9월 26일
편집: Image Analyst 2015년 9월 26일
Maybe use ismember() or any() or all()

댓글을 달려면 로그인하십시오.

카테고리

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

질문:

2015년 9월 26일

댓글:

2015년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by