p = randperm(n):
Matlab 2010a k . randperm code
[~, p] = sort(rand(1,n));
you will see that it is very easy to change it so that it permutes the melements n(now the result has size mx n):
[~, p] = sort(rand(m,n), 2);
Regardingp = randperm(n,k) :
I do not know how Matlab does this in this case, since my version does not support it. You can always do as described above and then crop:
p = p(:,1:k);
Not very effective for kmuch less n.
source
share