Matlab random number and while loop

조회 수: 10 (최근 30일)
Jack Huang
Jack Huang 2017년 2월 20일
댓글: Jack Huang 2017년 2월 20일
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

채택된 답변

Akira Agata
Akira Agata 2017년 2월 20일
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
Walter Roberson
Walter Roberson 2017년 2월 20일
Or, more simply,
while any(B)
Jack Huang
Jack Huang 2017년 2월 20일
Thank you so much for your input, it amazing how a small change to my code can make such a different!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by