Randperm is not returning all numbers and is duplicating some

2 views (last 30 days)
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!

Accepted Answer

Wayne King
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?
  2 Comments
Suzanne
Suzanne on 15 Mar 2013
I ran your code and it looks fine. It must be somewhere else in the code, then - and I thought I checked everything or that randperm had some specifications I wasn't aware of. Back to the drawing board!
Thanks, Wayne!
Suzanne
Suzanne on 15 Mar 2013
Drat! Just to finish this up, yes, it was an indexing problem. Good suggestion. Problem solved!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!