KTHCOMBN

版本 1.1.0.0 (2.3 KB) 作者: Jos (10584)
K-th combination(s) of elements
281.0 次下载
更新时间 2012/1/17

查看许可证

KTHCOMBN - the K-th combination of elements

M = KTHCOMBN(V,N,K) returns the K-th combination(s) of N elements of the elements in vector V. N is a positive scalar, and K is a vector of one or more integers between 1 and numel(V)^N.
V can be a vector of numbers, characters, cells strings, or even structures.

[M,IDX] = KTHCOMBN(V,N,K) also returns an index matrix, so that M = V(IDX).

Examples:
V = [7 31] , N = 3 , K = [7 4]
M = kthcombn(V, N, K)
% returns the 2-by-3 matrix:
% -> 31 31 7
% 7 31 31
% being the 7th and 4th combination (out of 9) of 3 elements of the V.

kthcombn('abcde',10, 5^9)
% -> aeeeeeeeee

V = cellstr(['a':'z'].') ; N = 5 ; K = [1 10000000 11881376] ;
M = kthcombn(V,N,K)
% returns the first, 10 millionth, and the last combination
% -> 'a' 'a' 'a' 'a' 'a'
% 'v' 'w' 'y' 'x' 'j'
% 'z' 'z' 'z' 'z' 'z'

All elements in V are regarded as unique, so M = KTHCOMBN([2 2],3, K) returns [2 2] for all values of K.

This function does the same as
M = COMBN(V,N)
M = M(K,:)
but it does not need to create all combinations first, before selecting, thereby avoiding some obvious memory issues with large values of N.
Beware of round-off errors for large values of N and K (see INTMAX).
For V = [0 1], KTHCOMBN returns a similar results as dec2bin(K-1,N)-'0'

See also nchoosek, perms
and combn, allcomb, nchoose on the File Exchange

引用格式

Jos (10584) (2024). KTHCOMBN (https://www.mathworks.com/matlabcentral/fileexchange/33922-kthcombn), MATLAB Central File Exchange. 检索来源 .

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

启发作品: KCOMBSN

Community Treasure Hunt

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

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

check K > 0 instead of K >= 0; fixed some spelling in the help

1.0.0.0