Azzera filtri
Azzera filtri

having two conditions for if statements

589 visualizzazioni (ultimi 30 giorni)
Christiana Won
Christiana Won il 4 Feb 2016
Modificato: Stephen23 il 4 Feb 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 Commento
Stephen23
Stephen23 il 4 Feb 2016
Modificato: Stephen23 il 4 Feb 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:

Accedi per commentare.

Risposte (1)

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

Community Treasure Hunt

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

Start Hunting!

Translated by