Can randperm() generate several random permutations?

matlab

Solution

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.

Problem

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!

Original source