How to run randperm command for 100 times and store these permutations
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi, I want to run randperm command to generate different permutations and save these permutation . The following code oly shows the last permutation. k contains only one permutation
N=10;T=4;
k=zeros(10,4);
for ii=1:T-1
Z = arrayfun(@(x)randperm(N),ii,'UniformOutput',0);
Z = cell2mat(Z);
k=Z;
end
댓글 수: 0
채택된 답변
Voss
2022년 4월 18일
To save each permutation, assign it to a row or column of k, instead of overwriting k each time.
You've set up k to have 10 rows for permutations of length 10, so it looks like you want each permutation to be a column of k:
N=10;T=4;
% k=zeros(10,4);
k=zeros(N,T);
for ii=1:T-1
% Z = arrayfun(@(x)randperm(N),ii,'UniformOutput',0);
% Z = cell2mat(Z);
Z = randperm(N); % this does the same thing as the two lines above
k(:,ii)=Z;
end
k
You can make each permutation be a row of k instead (here making T permutations instead of T-1):
N=10;T=4;
k=zeros(T,N);
for ii=1:T
k(ii,:)=randperm(N);
end
k
And this type of thing may have been what you were going for with arrayfun/cell2mat:
N=10;T=4;
k = cell2mat(arrayfun(@(x)randperm(N),1:T-1,'UniformOutput',0).')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!