Hello, greetings! I have the following array, A= 1:7. I want to get all the possible permutations of seven elements and I am trying to get my answer like this
ans =
1 2 3 4 5 6 7
1 2 5 3 4 7 6
2 3 4 5 1 7 6...... etc
It should be easy. But in the 'A' matrix, 1 and 2 indicates the simillar thing; 3 and 4 indicates the simillar thing; 5, 6, and 7 indicates the simillar thing. So, in the 'ans' 1 2 3 4 5 6 7 and 2 1 4 3 7 6 5 will be same. I want the code to return the values without repitating it. Is there any way to do this?
note: I want this code to do linear indexing. So, I can't replace A with any other matrix. It has to be 1:7.

 채택된 답변

Bruno Luong
Bruno Luong 2022년 10월 16일

0 개 추천

Just brute force of filter out what is considered as duplicated
g = [1 1 2 2 3 3 3];
x = 1:7;
p = perms(x);
[~,i] = unique(perms(g),'rows');
p = p(i,:);
p
p = 210×7
2 1 4 3 7 6 5 2 1 4 7 3 6 5 2 1 4 7 6 3 5 2 1 4 7 6 5 3 2 1 7 4 3 6 5 2 1 7 4 6 3 5 2 1 7 4 6 5 3 2 1 7 6 4 3 5 2 1 7 6 4 5 3 2 1 7 6 5 4 3

댓글 수: 5

Sourov Kumar Mondal
Sourov Kumar Mondal 2022년 10월 16일
Thanks a lot. This works just fine. Appreciate your effort.
Sourov Kumar Mondal
Sourov Kumar Mondal 2022년 10월 16일
Hi, if I want to use only 4 out of this 7 to make my permutations what modifications needed to be done in the code? ( same conditions as before: 1,2 are same; 3,4 are same; 5,6,7 same). The answer will be like this:
1 3 5 7
4 6 7 2 etc....
Please don't change your problem (even when you think its minor) in the future, it is count productive and induce waste of time for people who are trying to help you.
g = [1 1 2 2 3 3 3];
x = 1:7;
k = 4;
p = lperms(x,k);
[~,i] = unique(lperms(g,k),'rows');
p = p(i,:);
p
p = 62×4
2 1 4 3 2 1 3 5 2 1 5 3 2 1 6 5 2 4 1 3 2 3 1 5 2 4 3 1 1 4 3 5 2 3 5 1 1 4 5 3
function p = lperms(x, k)
p = nchoosek(x,k);
q = perms(1:k);
n = size(p,1);
[I,J] = ndgrid(1:n,q-1);
p = reshape(p(I + n*J),[],k);
end
Sourov Kumar Mondal
Sourov Kumar Mondal 2022년 10월 16일
Thanks again for the answer!
Bruno Luong
Bruno Luong 2022년 10월 16일
편집: Bruno Luong 2022년 10월 16일
Simplify lperms
function p = lperms(x, k)
p = nchoosek(x,k);
p = reshape(p(:,perms(1:k)),[],k);
end

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

추가 답변 (1개)

KSSV
KSSV 2022년 10월 16일

0 개 추천

A = {[1 2], [3 4], [5 6 7]} ;
iwant = perms(A)
iwant = 6×3 cell array
{[5 6 7]} {[ 3 4]} {[ 1 2]} {[5 6 7]} {[ 1 2]} {[ 3 4]} {[ 3 4]} {[5 6 7]} {[ 1 2]} {[ 3 4]} {[ 1 2]} {[5 6 7]} {[ 1 2]} {[5 6 7]} {[ 3 4]} {[ 1 2]} {[ 3 4]} {[5 6 7]}

댓글 수: 2

Sourov Kumar Mondal
Sourov Kumar Mondal 2022년 10월 16일
Thanks for the answer. But this is keeping 1 and 2; 3 and 4; 5, 6, and 7 together always. The output I am looking for is just like normal permutations of 7 elements. but 1 and 2; 3 and 4; and 5,6 and 7 will behave like simillar elements. The no. of expected permutations would be (7! / (2! * 2! * 3!)). Is there any solution to this? thanks is advanced.
Sourov Kumar Mondal
Sourov Kumar Mondal 2022년 10월 16일
Some expected outputs would be like this
1 3 5 7 6 4 2
1 4 3 5 6 2 7.... etc
But there won't be 2 3 5 7 6 1 because 1 and 2 are simillar

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

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 10월 16일

편집:

2022년 10월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by