VChooseKRO

バージョン 1.0.0.0 (10.8 KB) 作成者: Jan
Choose K elements from a vector with repetitions and order [MEX]
ダウンロード: 1.3K
更新 2010/1/1

ライセンスの表示

VChooseKRO(V, K) creates a matrix, which rows are all permutations created by choosing K elements of the vector V with order and with repetitions.

INPUT:
V: Array of class DOUBLE, SINGLE, (U)INT8/16/32/64, LOGICAL, CHAR.
Prefer (U)INT8 or (U)INT16 for speed.
K: Number of elements to choose.

OUTPUT:
Y: Matrix of size [NUMEL(V)^K, K].
Y has the same class as the input V.

EXAMPLES:
Choose 2 elements from [1,2,3]:
VChooseKRO(1:3, 2) ==> [1,1; 1,2; 1,3; 2,1; 2,2; 2,3; 3,1; 3,2; 3,3]
For speed cast the input to integer types if possible:
Y = double(VChooseKRO(int16(1:1000), 2));
is faster than:
Y = VChooseKRO(1:1000, 2);
To get the permutations of cell arrays, permute the index:
C = {'a', 'b', 'c', 'd'};
C2 = C(VChooseKRO(1:4, 2))
==> C2 = {'a', 'b'; 'a', 'c'; 'a', 'd'; 'b', 'c'; 'b', 'd'; 'c', 'd'}

This MEX version is faster than NPERMUTEK and the more general COMBINATOR of Matt Fig: ~8 times for small arrays, ~2 times for (N=2000, K=2), ~10 times for (N=2, K=16, Class=INT8). COMBN of Jos is again slower than Matt's functions and it does not handle INT classes. See screen shot.

Tested: Matlab 6.5, 7.7, 7.8, WinXP, Compilers: BCC5.5, LCC2.4/3.8, Open Watcom 1.8
The unit-test TestVChooseKRO should be called after compiling. It compares the speed of VChooseKRO with COMBN, NPERMUTEK and COMBINATOR, if these files are found in the path.

See also: VChooseK (no repetitions, no order):
http://www.mathworks.com/matlabcentral/fileexchange/26190

I'd appreciate suggestions for improvements and bug reports sent through email - thanks.

引用

Jan (2024). VChooseKRO (https://www.mathworks.com/matlabcentral/fileexchange/26242-vchoosekro), MATLAB Central File Exchange. に取得済み.

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

ヒントを得たファイル: permn, N_PERMUTE_K, COMBINATOR -combinations AND permutations

ヒントを与えたファイル: VChooseKO

Community Treasure Hunt

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

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