How to give IF condition in following code?

1 回表示 (過去 30 日間)
Abhinav
Abhinav 2014 年 11 月 6 日
コメント済み: Abhinav 2014 年 11 月 7 日
I have following code. It is generating some random values of y (0 or 1). I want it to select at least 2 ones's(1) and it it should never select all zeros (0).
for i=1:W
y = randi([0 1]);
QTWM2 = round(((1000-500)*(rand(1)))+500);
WSCT= y* round(((1500-1000)*(rand(1)))+1000);
WCST1(i)=WSCT;
QTWM1(i)=QTWM2;
f11(i) = y;
f1(i)=CFWT*y;
end
Please provide me with the solution
  4 件のコメント
Michael Haderlein
Michael Haderlein 2014 年 11 月 6 日
Actually, it's difficult to understand the question itself. Your code creates a bunch of random values, between 500 and 1000 (QTWM), between 1000 and 1500 plus 0 (WSCT), either 0 or 1 (f11) and either 0 or CFWT (f1). So what is now the question? Out of which array(s) should the values be chosen? How many values should be chosen?
Abhinav
Abhinav 2014 年 11 月 7 日
In this code 'y' will generate an array depending upon the value of 'W'. Let W = 5 so it will generate an array of 0 & 1 for 5 places. Suppose it generates [0 0 0 1 1]. I want that whatever the value of W it should only execute the loop if it has at least 2 ones in it. For eg for W=6 [0 0 0 0 0 1] is unacceptable but [0 0 0 0 1 1] is acceptable. I have no concern with other values. If it satisfies the condition only then it should display result to me otherwise run again

サインインしてコメントする。

採用された回答

Roger Stafford
Roger Stafford 2014 年 11 月 6 日
編集済み: Roger Stafford 2014 年 11 月 6 日
b = true;
while b
yt = randi([0,1],W,1);
b = (sum(yt)<2); % Repeat if 'yt' has less than two ones
end
for i=1:W
y = yt(i);
QTWM2 = ....
....
The while-loop will seldom have to repeat itself.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Translated by