How to run randperm command for 100 times and store these permutations
4 次查看(过去 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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!