random permutation with no matches to the position
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
その他の回答 (1 件)
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.
Take a look at this old thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/302075#820677
There are several implementations on the file exchange. I will promote my own ;-) http://www.mathworks.com/matlabcentral/fileexchange/30189-randpermfull
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!