Sampling with periodic replacement
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
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 Exchange에서 Biological and Health Sciences에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!