필터 지우기
필터 지우기

random permutation with no matches to the position

조회 수: 1 (최근 30일)
Valeria
Valeria 2016년 2월 25일
댓글: Valeria 2016년 2월 25일
Hello everyone,
I have a question. When using randperm function I would like to get only those permutations where no i number appears on the position i in the vector. Does anybody have a suggestion on how to do so?
Example: 1 2 3 4 5
acceptable permutation: 3 1 5 2 4
non-acceptable permutation: 1 3 5 2 4
Thanks a lot in advance!
  댓글 수: 2
Stephen23
Stephen23 2016년 2월 25일
편집: Stephen23 2016년 2월 25일
Brute force derangement:
N = 9;
out = randperm(N);
while any(out==(1:N))
out = randperm(N);
end
Valeria
Valeria 2016년 2월 25일
great, thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2016년 2월 25일
편집: Torsten 2016년 2월 25일

추가 답변 (1개)

Jos (10584)
Jos (10584) 2016년 2월 25일
편집: Jos (10584) 2016년 2월 25일
This specific type of permutation is called a derangement, and there are many methods to generate one. However, it is no so easy to come up with an elegant and fast algorithm.
There are several implementations on the file exchange. I will promote my own ;-) http://www.mathworks.com/matlabcentral/fileexchange/30189-randpermfull

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by