all possible permutations
1 view (last 30 days)
Show older comments
Hi Guys, any chance someone know how to get all possible permutations from a set of numbers?
e.g. [1 -1 2 -2 3 -3 0] but I would need to use only 4 numbers at the time, so there should be 840 possible permutations,... problem is that "perms" takes all 7 numbers into account at the time,...
is there a way matlab can do this?
many thanks, ondrej
1 Comment
Answers (7)
Fangjun Jiang
on 10 Nov 2011
Would this give you the correct result?
a=[1 -1 2 -2 3 -3 0];
b=nchoosek(a,4);
M=size(b,1);
c=cell(M,1);
for k=1:M
Temp=b(k,:);
c{k}=Temp(perms(1:4));
end
d=cell2mat(c);
0 Comments
Jonathan
on 10 Nov 2011
You can use this method. It does not use cell arrays, which are conceptually convenient but rather inefficient.
A = nchoosek([1 -1 2 -2 3 -3 0], 4);
P = perms(1:4);
B = zeros(size(A,1)*size(P,1),size(A,2));
for i = 1:size(P,1)
B(1 + size(A,1)*(i-1):size(A,1)*i,:) = A(:,P(i,:));
end
0 Comments
See Also
Categories
Find more on Logical 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!