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!