필터 지우기
필터 지우기

all possible permutations

조회 수: 2 (최근 30일)
Ondrej
Ondrej 2011년 11월 10일
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
Jan
Jan 2011년 11월 10일
Are you talking about permutations or combinations?

댓글을 달려면 로그인하십시오.

답변 (7개)

Dr. Seis
Dr. Seis 2011년 11월 10일
Or...
a = perms([1, -1, 2, -2, 3, -3, 0]);
b = a(:,1:4);
c = unique(b,'rows');
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 11월 10일
+1! That should work and it's easier!
Jonathan
Jonathan 2011년 11월 10일
This definitely works. However, it uses a lot more time and space than it needs to. It uses 5040 rows for a and b and 7 columns for a.

댓글을 달려면 로그인하십시오.


Jan
Jan 2011년 11월 10일
If you search in the FEX, you will find this nice tool: FEX: combinator.

Ondrej
Ondrej 2011년 11월 10일
talking about permutations not combinations,...
nchoosek([1, -1, 2, -2, 3, -3, 0],4) will give you combinations mate, but thanks anyway,...
how no idae what is FEX, but I guess I need to give it a go,... many thanks,...

Fangjun Jiang
Fangjun Jiang 2011년 11월 10일
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);

Ondrej
Ondrej 2011년 11월 10일
thank you very much! very nice! you done thinking I should have done,... thanks a lot,... I kind of hoped that matlab has a built-in function which I don't know about,... thanks a lot again!

Ondrej
Ondrej 2011년 11월 10일
thank you mate! really nice! apparently I'm not that great with matlab,... cheers,..

Jonathan
Jonathan 2011년 11월 10일
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by