An efficient function for k-th combination with no repetition?

I am looking for a function that accepts a vector V of n elements with no repetition and integers k, i, and returns the i-th combination (in lexicographic order) of k elements of V. It is essential the function do not generate all the combinations and then select the i-th.
Example: V=[1 2 3 4 5], k= 3, i=6. Expected result is: [1 4 5].
Why? Beacuse all combinations of 3 elements of V ordered lexicographically are: 123, 124, 125, 134, 135, 145, 234, 235, 245, 345. And, so the 6-th is 145.

2 个评论

Question for clarification.
Should the order in a vector V be kept in the output?
Because, if not, all combinations of 3 elements will be 123, 124, 125, 132, 134,.... etc.
It should be. The combinations 123, 132, etc, are identified.

请先登录,再进行评论。

 采纳的回答

V = [1 2 3 4 5];
K = 3;
M = 6;
comb = onecomb(numel(V), K, M); % Replies the indices
result = V(comb) % The elements of V

3 个评论

Yes, that's it. I did not know about onecomb.m, but I have found it on net. Thx!
A search in the FileExchange is a good idea. You found the file in the net? You can use the link directly.
I arrived at the same location :) I am new here.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品

版本

R12.1

Community Treasure Hunt

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

Start Hunting!

Translated by