choosing K elements out of N elements

Hi,
Is there any function which acts as the following: given a set of n elements, it returns all the k-chosen combinations such that these k elements have at a distance of at most P? For example out of 1:100 we want to chose 5 elements such that they have a maximum distance of 10. how can we do it? btw I need a fast function
Thanks a lot

2 Comments

Do you seriously expect that such a code already exists?
First, define what you mean by the word distance. I can think of at least 2 meanings.
As for being fast, everybody wants fast code. I have never actually seen someone say that they explicitly want slow code, or that they don't really care if the code is fast or incredibly slow.
:D I mean that if for example you have 3 numbers and you want their distance to be at most 10, they should be chosen such that any two of them are at most 10 numbers apart

Sign in to comment.

 Accepted Answer

Jan
Jan on 20 Mar 2016
Depending on the fact, if you want repetitions or not, you can use FEX: VCHooseKR or FEX: VChooseK.
If the maximum distance between the elements should be 10, you can solve the problem for the chunk 1:11 at first and in a second step add 0:89 to the result. bsxfun will be useful for the last step.

3 Comments

Thanks for your answer. This is actually what I was thinking of, but it requires yout function FEX: VChooseK to be called in a MATLAB loop which may make it much slower. I was wondering if someone has had the same problem before and maybe developed the whole code for it. Btw, I tried to use your code, but the compiling seemed really complicated. (I downloaded SDK 7.1 and installed it but it still didn't work.)
Thanks a gain for ur time
Yes, unfortunately compiling is not trivial.
If you explain, if you need repetions or not, I could perhaps offer a specific code. You have to call the code for the permutation once only:
B = VChooseK(1:11, 5);
Result = bsxfun(@plus, reshape(B, [1, size(B)], (0:89).');
Result = reshape(Result, [], 5);
In this case Matlab's slower nchoosek would solve youtr problem also. Using uint8 values would accelerate the computations. If you cannot get the compilation to work, FEX: Combinator works, but slower.
Thanks, I just used Combinator in a loop as you mentioned. Btw don't you have a compiled version of Vchoosek?

Sign in to comment.

More Answers (0)

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!