How to generate all possible permutations without using the function perms/randperm?
이전 댓글 표시
Hello
I am trying to generate all possible permutations (permMat) of a matrix, say A = [1:4], without using the function perms as that is not allowed, but I am stuck.
So far I have came up with
while size(permMat,1) ~= factorial(4)
%to find a random permutation of A
while length(r3)~= 4 %where r3 is a random permutation of A
x=randi([1,C]);
if sum(r3==x)==0
r3=[r3 x];
end
end
permMat = [permMat;r3];
permMat = unique(permMat,'rows');
end
However, I realised that using this above method to find a random permutation will only result in the same permutation in the loop. Is there anyway to generate a always-changing random permutation in the loop? Or is there a better way to generate all possible permutations?
Thanks!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!