having two conditions for if statements

952 visualizaciones (últimos 30 días)
Christiana Won
Christiana Won el 4 de Feb. de 2016
Editada: Stephen23 el 4 de Feb. de 2016
I have x= randi ([0,1],1,8, which is a 1 by 8 matrix of 0 or 1 randomly distributed and s= sum (x,2).
I wanted to write a code where
if S =1 or 2 or 3, and X(1) =0,then, Y= 100/S, elseif, S= 1 or 2 or 3, and X(1)=1, then Y=0,
But I got and error for using and/ &/ &&.
SO this is basically the code I tried using (for && on the first row, I tried 'and' and & as well).
if S == 1||2||3, && X(1) == 0,
then Y ==100/ S;
elseif S == 1||2||3, AND X(1) ==1,
THEN Y== 0 ;
end
help me please!
  1 comentario
Stephen23
Stephen23 el 4 de Feb. de 2016
Editada: Stephen23 el 4 de Feb. de 2016
@Christiana Won: You had the same problem in a comment to your last question:
You have again forgotten to take into account operator precedence, which was clearly explained to you in your last question.
MATLAB's relational operations are binary operators, so they can only handle two inputs at once. Read this to know more:

Iniciar sesión para comentar.

Respuestas (1)

Roger Wohlwend
Roger Wohlwend el 4 de Feb. de 2016
if (S == 1) || (S == 2) || (S == 3)
if (X(1) == 0)
Y = 100 / S;
else
Y = 0;
end
end

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by