How to run randperm command for 100 times and store these permutations
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lilly lord
el 18 de Abr. de 2022
Comentada: Voss
el 19 de Abr. de 2022
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 comentarios
Respuesta aceptada
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
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).')
2 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!