How to generate all possible permutations without using the function perms/randperm?
Show older comments
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!
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!