if statement and "equal to" problem
Mostrar comentarios más antiguos
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 "~=" ?
Respuestas (2)
Image Analyst
el 26 de Sept. de 2015
0 votos
Use double ampersand, &&, instead of the word AND.
2 comentarios
Image Analyst
el 26 de Sept. de 2015
and double vertical bar instead of the work "OR".
John D'Errico
el 26 de Sept. de 2015
Funny. I completely missed the use of OR and AND in that line of code. Again, not valid MATLAB syntax.
John D'Errico
el 26 de Sept. de 2015
Editada: John D'Errico
el 26 de Sept. de 2015
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 comentario
Image Analyst
el 26 de Sept. de 2015
Editada: Image Analyst
el 26 de Sept. de 2015
Maybe use ismember() or any() or all()
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!