Fast batch generation of permutations

バージョン 1.0.0.0 (4.56 KB) 作成者: Peter Li
Get next block of lexicographic permutations of input vector
ダウンロード: 420
更新 2013/12/23

ライセンスの表示

I simply wrapped the C++ STL function next_permutation in Mex. To use, first compile for your system with 'mex nextperms.cpp'.

See nextperms.m and nextperms_example_script.m for documentation.

Typical use case would be you need to iterate through a large number of permutations that will not all fit in memory at once. You could call this in a loop, requesting blocks of 1M permutations each or so.

(This is also significantly faster than Matlab's perms on my system just for generating all permutations, although it does not generate them in the same order.)

You pass in an initial vector and a number of next permutations to generate. If the first input isn't a vector it is treated as a vector column-wise. You can use any numeric type (if you want long long / int64 you have to uncomment some code lines).

The permutations are returned as a matrix columnwise. They are generated in lexicographic order (as per STL spec). The initial vector passed in is not included in the output; output starts at the next permutation after the input vector.

** IMPORTANT **
There is currently no check for when the permutations wrap back around to the initial permutation, so it is the user's responsibility to check for this or only request an appropriate number of permutations.

If you're not sure how to use this after looking at nextperms.m and nextperms_example_script.m, try just running it on some small inputs and inspecting the results: nextperms(3:-1:1, 4); nextperms(3:-1:1, factorial(3)); nextperms(3:-1:1, factorial(3)*2);

引用

Peter Li (2024). Fast batch generation of permutations (https://www.mathworks.com/matlabcentral/fileexchange/44785-fast-batch-generation-of-permutations), MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2012a
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersMATLAB Compiler についてさらに検索
謝辞

ヒントを得たファイル: nextperm, Next Combination/Permutation

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.0.0.0