Trouble getting two conditions to work with array While loop

I'm having problems getting my while loop to work with two conditions. I'm trying to create an array that contains the numbers 3 and 7, randomizing the numbers again if it doesn't contain those two. Right now, I have this. Any ideas? Thanks!
a = zeros(1,6);
a(1,1:6) = randperm(8,6)
i = 0;
while (a ~= 3 & a ~= 7)
i = i+1;
a(1,1:6) = randperm(8,6);
end

2 件のコメント

madhan ravi
madhan ravi 2018 年 9 月 1 日
can you type your required output as an example?
Peter Lindquist
Peter Lindquist 2018 年 9 月 1 日
I'm just looking for an array of 6 random numbers that includes 3 and 7. so for example, a = 8 5 3 2 7 1.

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

 採用された回答

Image Analyst
Image Analyst 2018 年 9 月 1 日

1 投票

Here is one way that uses a while loop:
a(1:6) = randperm(8,6)
k = 0;
maxIterations = 200; % Whatever, the failsafe
while k <= maxIterations && sum(a == 3 | a == 7) < 2
k = k+1;
a = randperm(8, 6);
end
a % Show in command window.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by