Sampling with periodic replacement

조회 수: 3 (최근 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 CenterFile Exchange에서 Biological and Health Sciences에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by