Randperm is not returning all numbers and is duplicating some
2 views (last 30 days)
Show older comments
Essentially, I need to produce a list of numbers. The first 24 are the numbers 1 through 24, in a random order. The next 24 (numbers 25-48 of the big list) are also the numbers 1 through 24, in a different random order. This gets repeated 15 times.
I have set up code that initially seemed to work using a for loop and randperm. I repeatedly ask randperm to randomize the numbers 1 through 24 and I add those into my list. However, when examining the list more closely, the first few sets of 24 are fine, but then randperm breaks down a bit. In the third iteration of randperm, I get the number 21 repeated twice and I've lost the number 3. The next time through I have two 6's, but no 22.
Any guesses about why this might be happening? Are there constraints I can enter for randperm to ensure this doesn't happen?
Thanks!
0 Comments
Accepted Answer
Wayne King
on 15 Mar 2013
Edited: Wayne King
on 15 Mar 2013
I don't see it repeating any elements.
Y = zeros(24,15);
for nn = 1:15
tmp = randperm(24);
Y(:,nn) = tmp;
len(nn) = length(unique(Y(:,nn)));
end
Look at the columns of Y. And look at len, that vector is all 24s.
Is it possible that you are making a mistake in the indexing?
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!