How to run randperm command for 100 times and store these permutations
7 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 18 Apr 2022
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).')
More Answers (0)
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!