How to run randperm command for 100 times and store these permutations

4 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Voss
Voss el 18 de Abr. de 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
k = 10×4
1 4 5 0 2 3 1 0 6 9 6 0 5 8 7 0 4 1 4 0 10 6 3 0 8 10 2 0 9 7 8 0 7 2 9 0 3 5 10 0
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
k = 4×10
10 5 9 1 7 8 4 2 3 6 10 4 9 5 3 2 1 8 7 6 2 9 8 1 5 6 4 3 7 10 7 9 3 2 10 6 1 8 4 5
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).')
k = 3×10
1 2 5 3 8 4 6 10 7 9 2 7 3 8 6 9 1 10 4 5 3 2 8 1 10 7 6 9 5 4

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by