having two conditions for if statements

조회 수: 962 (최근 30일)
Christiana Won
Christiana Won 2016년 2월 4일
편집: Stephen23 2016년 2월 4일
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
Stephen23
Stephen23 2016년 2월 4일
편집: Stephen23 2016년 2월 4일
@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:

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

답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by