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 件のコメント

John D'Errico
John D'Errico 2016 年 3 月 20 日
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.
Sepehr Janghorbani
Sepehr Janghorbani 2016 年 3 月 21 日
: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

サインインしてコメントする。

 採用された回答

Jan
Jan 2016 年 3 月 20 日

0 投票

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 件のコメント

Sepehr Janghorbani
Sepehr Janghorbani 2016 年 3 月 20 日
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
Jan
Jan 2016 年 3 月 21 日
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.
Sepehr Janghorbani
Sepehr Janghorbani 2016 年 3 月 21 日
Thanks, I just used Combinator in a loop as you mentioned. Btw don't you have a compiled version of Vchoosek?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeConway's Game of Life についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by