Sampling with periodic replacement

7 ビュー (過去 30 日間)
Matt J
Matt J 2018 年 12 月 31 日
編集済み: Walter Roberson 2019 年 1 月 1 日
I am looking for an efficient way of doing randperm(n,k) many successive times with the same n and k. Can anyone propose something more efficient than the obvious for-loop approach below?
M=1e5;
n=100;
k=10;
A=nan(k,M);
for i=1:M
A(:,i) = randperm(n,k).';
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 12 月 31 日
In current versions of MATLAB, randperm with small k relative to n (and perhaps other cases) uses a Fisher-Yates shuffle for efficiency.
Older versions of MATLAB use sort(rand) to extract orderings. That can be extended easily:
[~, A] = sort( rand(n, M) );
  2 件のコメント
Matt J
Matt J 2019 年 1 月 1 日
編集済み: Matt J 2019 年 1 月 1 日
Ah! Thanks. I suppose we should use mink/maxk, though, for Matlab versions that have it
[~, A] = mink( rand(n, M) , k ); ?
Walter Roberson
Walter Roberson 2019 年 1 月 1 日
編集済み: Walter Roberson 2019 年 1 月 1 日
Good idea. On older systems, you would use A(1:k,:)
... Though I just did some timing tests, and using sort and indexing turns out to be measurably faster. Using sort and indexing is bout 0.125 for the parameters you indicate, vs about 0.156 for using mink.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by