Matlab random number and while loop

13 vues (au cours des 30 derniers jours)
Jack Huang
Jack Huang le 20 Fév 2017
Commenté : Jack Huang le 20 Fév 2017
Assume there are N different cards, the question is how many cards will I need to draw out before i get a full set of cards. this is what i've got so far B=[1:N]; draws=0;
while B~= zeros(1,N);
n=randi(N)
if n==B(n)
B(n)=0
draws=draws+1;
end
end
When i run this it gives only 1 value of n and always give me count=1 but i want this process to repeat untill I have only of each cards

Réponse acceptée

Akira Agata
Akira Agata le 20 Fév 2017
This is because B~= zeros(1,N) returns 1-by-N logical array.
Please revise the first line of your code to while any(B ~= zeros(1,N)) .
  2 commentaires
Walter Roberson
Walter Roberson le 20 Fév 2017
Or, more simply,
while any(B)
Jack Huang
Jack Huang le 20 Fév 2017
Thank you so much for your input, it amazing how a small change to my code can make such a different!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by