VChooseKR

版本 1.0.0.0 (13.9 KB) 作者: Jan
Choose K elements from a vector with repetitions and without order [MEX]
1.0K 次下载
更新时间 2010/1/5

查看许可证

VChooseKR(V, K) creates a matrix, which rows are all combinations created by choosing K elements of the vector V without 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 [(N+K-1 over K), K] and same class as V.

EXAMPLES:
Choose 2 elements from [1,2,3,4]:
VChooseKR(1:4, 2)
==> [1,1; 1,2; 1,3; 1,4; 2,2; 2,3; 2,4; 3,3; 3,4; 4,4]
For speed cast the input to integer types if possible:
Y = VChooseKR(int8(1:64), 3)
is 6 times faster than with DOUBLEs!
To get the combinations of cell arrays, use the combinations of the index:
C = {'a', 'b', 'c', 'd'};
C2 = C(VChooseKR(1:4, 2))
==> C2 = {'a','a'; 'a','b'; 'a','c'; 'a','d'; 'b','b'; 'b','c'; ...
'b','d'; 'c','c'; 'c','d'; 'd','d'}

This MEX version is faster than COMBINATOR of Matt Fig: E.g. ~7 times for K=64, N=2, ~140 times for K=2, N=128. The older PICK of Stefan Stoll is slower than COMBINATOR. See screen shot.

Tested: Matlab 6.5, 7.7, 7.8, WinXP, Compilers: BCC5.5, LCC2.4/3.8, Open Watcom 1.8
Please run the unit-test TestVChooseKR after compiling or to compare the speed with other functions from the FEX, if they are found in the path.

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

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

引用格式

Jan (2024). VChooseKR (https://www.mathworks.com/matlabcentral/fileexchange/26277-vchoosekr), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2009a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0