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 Comments

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.

Sign in to comment.

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R12.1

Asked:

on 20 Dec 2018

Edited:

on 21 Dec 2018

Community Treasure Hunt

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

Start Hunting!