multiple random samples of size k

I would like to generate M random samples, each of size k , from the integers 1:n without replacement.
(i.e. generate an M-by-k matrix of random integers, where each row contains integers in [1,n], and within a row, no integer is repeated).
I will use this matrix to perform Monte Carlo hypothesis testing.
I can easily write this in Matlab myself., however...
I would like to use an in-built Matlab function, but I am unable to find one. Does such a function exist in Matlab?
Note: Matlab's bootstrap do not fulfill the criteria I listed above.

回答 (3 件)

Walter Roberson
Walter Roberson 2015 年 6 月 25 日

1 投票

You could use randperm(n,k) in a loop. But randperm is not designed to generate an array. You can look into the randperm implementation and generalize it easily:
T = rand(M, n);
[~, order] = sort(T, 2);
randsamples = order(:,1:k);
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 25 日
編集済み: Azzi Abdelmalek 2015 年 6 月 25 日

0 投票

n=6
k=3
row1=randperm(n,k)
For a matrix with M rows
n=6
k=3
M=5
A=cell2mat(arrayfun(@(x) randperm(n,k),(1:M)','un',0))
Star Strider
Star Strider 2015 年 6 月 25 日

0 投票

Use the nchoosek function. It will do what you want. The randperm function is another option that may be more appropriate to your application.

2 件のコメント

cmo
cmo 2015 年 6 月 25 日
Thank you, but nchoosek will not work when n is large and k is non-trivial (the space of permutations is too big)
Star Strider
Star Strider 2015 年 6 月 25 日
My pleasure. The perms function has the same limitations, but it and nchoosek are the only ones I know of that produce matrices. Otherwise, you’re reduced to using a loop with randperm to create your matrix.

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

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

質問済み:

cmo
2015 年 6 月 25 日

コメント済み:

2015 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by