having two conditions for if statements

962 Ansichten (letzte 30 Tage)
Christiana Won
Christiana Won am 4 Feb. 2016
Bearbeitet: Stephen23 am 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 Kommentar
Stephen23
Stephen23 am 4 Feb. 2016
Bearbeitet: Stephen23 am 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:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by