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 Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by