MATLAB Answers

How can I get randperm to return a permutation of a vector that has no entries at their original positions?

140 ビュー (過去 30 日間)
Darcy Cordell
Darcy Cordell 2021 年 2 月 26 日
コメント済み: Bruno Luong 2021 年 3 月 3 日
I want take a random permutation of a vector such that all entries of the vector move to a new location.
For example, if I have a vector [1,2,3,4,5], then the following permutations are acceptable:
[2,1,4,5,3], [3,1,5,2,4], [5,4,2,3,1], etc.
However, for me, the following vector is not acceptable:
because the "3" has remained in the same location.
The "randperm" function in MATLAB allows for some of the entries in the vector to stay in the same position. Is there some way to use randperm that stops it from doing this? Or is there some other function out there that I am missing? (I have also looked at the functions "datasample" and "randsample" but they also do not seem to allow for this).
  2 件のコメント
Stephen Cobeldick
Stephen Cobeldick 2021 年 2 月 27 日
This type of permutation is called a derangement:
You can start by searching FEX:
and reading the descriptions of those submissions.


その他の回答 (4 件)

Bruno Luong
Bruno Luong 2021 年 2 月 28 日
編集済み: Bruno Luong 2021 年 2 月 28 日
Here is an implementation of a non-rejection method and unbiased random derangement:
function p = randder(n)
% p = randder(n)
% Generate a random derangement if length n
% n: scalar integer >= 2
% p: array of size (1 x n), such that
% unique(p) is equal to (1:n)
% p(i) ~= i for all i = 1,2,....n.
% Base on: "Generating Random Derangement", Martinez Panholzer, Prodinger
% Remove the need inner loop by skrink index table J (still not ideal)
% See also: randperm
p = 1:n;
b = true(1,n);
m = n-1;
J = 1:m;
i = n;
u = n;
utab = 1:n;
qtab = (utab-1).*subfactorial(utab-2)./subfactorial(utab);
overflowed = ~isfinite(qtab);
qtab(overflowed) = 1./utab(overflowed);
x = rand(1,n);
r = rand(1,n);
while u>=2
if b(i)
k = ceil(x(i)*m);
j = J(k);
p([i j]) = p([j i]);
if r(i) < qtab(u)
b(j) = false;
J(k:m-1) = J(k+1:m);
m = m-1;
u = u-1;
u = u-1;
i = i-1;
if J(m)==i
m = m-1;
end % randder
function D = subfactorial(n)
D = floor((gamma(n+1)+1)/exp(1));
It might be slower but it's a non rejection method, so at least the run-time is always predictable.
p =
7 6 1 3 9 5 10 4 2
Check validity and uniformity
m = 100000;
n = 6;
D=arrayfun(@(x) randder(n), 1:m, 'UniformOutput', false);
OK = all(U-(1:n),'all') && ...
~any(sort(U,2)-(1:n),'all'); % must be true
if OK
fprintf('All derangements are valid\n');
% Check uniformity
close all
nbins = min(1000,size(U,1));
  7 件のコメント
Bruno Luong
Bruno Luong 2021 年 3 月 3 日
I run de tic/toc up to 256 runs for each method and plot the histogram. Here is the results (one can see a hint of the power law (1-1/e)^p for runtime for rejection methods):
>> benchderangement
mean(t) = 1.432840
max(t) = 7.470663
min(t) = 0.463656
(max-min)/mean = 4.890291
mean(t) = 1.744887
max(t) = 2.063773
min(t) = 1.632292
(max-min)/mean = 0.247283
mean(t) = 0.592406
max(t) = 2.253336
min(t) = 0.229715
(max-min)/mean = 3.415937


Jeff Miller
Jeff Miller 2021 年 2 月 26 日
I don't think randperm can do that by itself, but I think this would work for an even number of items in the original vector:
orig = 1:10; % an example for the original vector of items
nvec = numel(orig);
halfn = nvec/2;
perm1 = randperm(nvec);
final = zeros(size(orig));
for ivec=1:halfn
swap1pos = perm1(ivec);
swap2pos = perm1(ivec+halfn);
final(swap1pos) = orig(swap2pos);
final(swap2pos) = orig(swap1pos);
If you have an odd number of items in the original vector, handle one item specially and use this method with the remaining ones.
Would not be surprised if someone has a better solution, though.

Image Analyst
Image Analyst 2021 年 2 月 27 日
Just keep looping until there are no matches, like this:
n = 5;
originalVector = 1 : n;
maxIterations = 10000;
loopCounter = 1;
while loopCounter < maxIterations
newVector = randperm(n);
matches = newVector == originalVector;
if ~any(matches)
break; % Break out of loop if there are no matches.
loopCounter = loopCounter + 1;
% Print out newVector to command window.
fprintf('Found answer after %d iterations.\n', loopCounter);
  5 件のコメント
Darcy Cordell
Darcy Cordell 2021 年 3 月 1 日
I think this is often the fastest solution for large N. But for small N, I think randpermfull is faster.


David Goodmanson
David Goodmanson 2021 年 2 月 28 日
編集済み: David Goodmanson 2021 年 2 月 28 日
Hi Darcy,
the methods I have seen here seem to involve trying randperm and rejecting the result if an element remains in the same location. Here is a method that uses the cycle structure of the permutation and does not allow any 1-cycles (element stays where it is). Randperm is called once. The code uses the fact that if you have n elements, and do a chain of randomly chosen elements starting with a given element, the odds that you obtain a k-cycle is 1/n for every k.
I don't know how the speed is compared to the rejection method, but this code is not slow, taking half a second for 10 million elements.
n = 100
q = randperm(n);
p = zeros(1,n);
nrem = n; % number of remaining elements
cycstruct = []; % cycle structure (just the lengths)
while nrem > 0
if nrem == 2;
cyc = 2;
cyc = randi(nrem-2)+1; % cycle length
if cyc == nrem-1; % unallowed cycle length
cyc = nrem;
cycstruct = [cycstruct cyc];
nrem = nrem-cyc;
ind = q(1:cyc);
q(1:cyc) = [];
p(ind) = circshift(ind,-1);
[1:n; p]; % p is the result
any(p==1:n) % check if any element stays at home
any(diff(sort(p))~=1) % any duplicated elements?
  2 件のコメント
David Goodmanson
David Goodmanson 2021 年 2 月 28 日
Hi Paul,
I agree with what you are saying. I had thought that the probability of getting a 4-cycle was the same as getiing a 2-cycle (and hence a pair of 2-cycles) but actually there are six cases of one 4-cycle and three cases of a pair of 2-cycles. So I will go look at that, but meanwhile Bruno has been on the job.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by