Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can randperm() generate several random permutations?

Tags:

matlab

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!

like image 788
Tim Avatar asked Mar 28 '13 14:03

Tim


People also ask

How do you generate random permutations?

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 ...

How do you generate random permutations in Python?

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.

How do you generate a random number in Matlab?

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.


1 Answers

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.

like image 154
yuk Avatar answered Oct 17 '22 10:10

yuk