Generating all ordered samples with replacement
古いコメントを表示
Hello everybody,
is there a function in Matlab which generates an array containing all ordered samples of length k taken from a set of n elements
, that is all the k-tuples
where each
can be any of the
, and whose total number is
?
, that is all the k-tuples
where each
can be any of the
, and whose total number is Or can anybody suggest a simple code to generate all of them? I am guessing it involves the iterative use of datasample function checking that every new generated sample is different from the previous ones, but I couldn't find so far a satisfactory way to write it
採用された回答
その他の回答 (2 件)
For
and
,
n = 20;
k = 5;
result = dec2base(0:n^k-1, n); %generate all n^k samples with replacement, as char vector 0-9 + A-Z
result = result - '0' + 1; %convert character to numbers 1-10, A-Z get converted to 18+
result(result>17) = result(result>17) - 7 %convert 18+ to 11+
For greater n you'll have to use Jan's answer.
Riccardo
2019 年 5 月 8 日
3 件のコメント
No, that's going to be extremely slower than both solutions proposed. You're going to be wasting time generating many permutations that were already generated. Both solutions proposed just enumerate all the combinations, with no guessing.
With your solution, when there's only one row left to generate, the probability that it is returned by datasample will be
, extremely small. It's going to take a long time before that last row is generated.
Riccardo
2019 年 5 月 8 日
Jan
2019 年 5 月 8 日
Yes, it is formally correct.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!