How does randperm with 2 arguments work internally?

Hello, I need to generate an efficient c code where I need to use randperm with 2 arguments. I could figure out from other answer how randperm with single argument works but randperm with 2 arguments seems to be more complex. At some places it uses more than 2 random numbers per value. My guess was that it first randomly chooses an element from the array and then places it in a random place in output, but I was wrong. I didn't get the same output on same seed.

답변 (1개)

Roger Stafford
Roger Stafford 2016년 5월 16일
편집: Roger Stafford 2016년 5월 16일
[~,p] = sort(rand(n,1));
p = p(1:k);
where p contains k unique values chosen from 1:n. Of course Mathworks may well use a different method by now.

댓글 수: 2

Hey, thanks for reply but no this is not the method MATLAB uses. It is not even efficient. For any k it will take nlog(n) time to compute the permutation.
This is the method that MATLAB used until recently; the source for randperm was completely open and most of the regular volunteers looked at it. We do not know what routine is used now, as Mathworks went to some undocumented internal implementation.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2016년 5월 16일

댓글:

2016년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by