choosing n set from all possible permutations
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
Star Strider
on 14 May 2014
Edited: Star Strider
on 14 May 2014
3 Comments
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
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)
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
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?
See Also
Categories
Find more on Logical 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!