choosing n set from all possible permutations

2 views (last 30 days)
Abed
Abed on 14 May 2014
Commented: Image Analyst on 14 May 2014
assume I have a vector 1 by 100. I want to choose 5 possible permutations (each 1 by 100)? (hint: from all possible permutations I need five of them)...how? tnx

Answers (3)

the cyclist
the cyclist on 14 May 2014
One of many possible ways to do this is using the randperm() command.
  1 Comment
Abed
Abed on 14 May 2014
thanks...but lets say I have A = [2 5 7 11 13] then I need five different samples each 1 by 5, with permutation from A. how?

Sign in to comment.


Star Strider
Star Strider on 14 May 2014
Edited: Star Strider on 14 May 2014
Use perms.
v = [2 5 7 11 13];
p = perms(v)
Choose any five rows of p.
  3 Comments
Star Strider
Star Strider on 14 May 2014
Edited: Star Strider on 14 May 2014
So far, you’ve ruled out randperms and perms. Seems you’ll have to write your own function.
One way might be to preallocate a (100x100) matrix, then take your 100-element vector and select 100 random elements from it without repetitions, then do that for each subsequent row of your matrix, checking that each new row is not a repetition of any previous row. (This will likely take a while to run.) Then, choose five rows.
For the row constructions, the unique function might be your friend. That will help you detect and replace any repetitions.
Just subtract your current row from previous rows to see if they're unique (although the unique function might help you there, too). If any difference is zero, repeat, if not, create the next row.
Star Strider
Star Strider on 14 May 2014
If repetitions are allowed and you only need 10 rows of 100 numbers, they’re simply random numbers.
This works:
M = randi(100, 10, 100)

Sign in to comment.


Image Analyst
Image Analyst on 14 May 2014
Try this:
clc; workspace;
% Create the original 1 by 100 vector.
v = randi(99,[1,100])
% Now get 5 new vectors which are v scrambled
% into different permutations.
for k = 1 : 5
newOrder = randperm(100);
newV = v(newOrder)
end
You can have newV be a 2D array if you want:
% Create the original 1 by 100 vector.
v = randi(99,[1,100])
% Now get 5 new vectors which are v scrambled
% into different permutations.
for k = 1 : 5
newOrder = randperm(100);
newV(k, :) = v(newOrder)
end
  3 Comments
Abed
Abed on 14 May 2014
I did it for smaller set just for simplicity. clc clear all % Create the original 1 by 100 vector. v = [2 3 5 6 11] % Now get 5 new vectors which are v scrambled % into different permutations. for k = 1 : 5 newOrder = randperm(5); newV = v(newOrder) end
these sets are without replacement. I need with replacement. for example for this case I expect to have [11 2 2 5 5] as well.
Image Analyst
Image Analyst on 14 May 2014
I don't know what with or without replacement means in this context. I just scrambled the order of the existing elements. That's what we've all suggested - isn't that what you want?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!