Trouble getting two conditions to work with array While loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Peter Lindquist
am 1 Sep. 2018
Kommentiert: Peter Lindquist
am 1 Sep. 2018
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 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Sep. 2018
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!