In Matlab
p = randperm(n,k) returns a row vector containing k unique integers selected randomly from 1 to n inclusive.
Can one call to randperm() return several rows of vectors, each of which is as above? If not, is there other way to generate several random permutations?
Will avoiding loop necessarily be faster in this case?
Thanks!
A simple algorithm to generate a permutation of n items uniformly at random without retries, known as the Fisher–Yates shuffle, is to start with any permutation (for example, the identity permutation), and then go through the positions 0 through n − 2 (we use a convention where the first element has index 0, and the ...
To generate random Permutation in Python, then you can use the np random permutation. If the provided parameter is a multi-dimensional array, it is only shuffled along with its first index. If the parameter is an integer, randomly permute np.
Use the rand , randn , and randi functions to create sequences of pseudorandom numbers, and the randperm function to create a vector of randomly permuted integers. Use the rng function to control the repeatability of your results.
RANDPERM itself returns only one permutation. If you want to avoid loop you can call it with ARRAYFUN:
Nperm = 5;
N = 6;
result = arrayfun(@(x)randperm(N),(1:Nperm)','UniformOutput',0);
This will return Nperm x 1 cell array. To convert it to matrix you can use CELL2MAT:
result = cell2mat(result);
There is also PERMS function that returns all permutations, but it only practical for a small numbers.
Check also FileExchange submissions ALLCOMB, PERMS and others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With