How to give IF condition in following code?
1 次查看(过去 30 天)
显示 更早的评论
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
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?
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!