How to Permute the array with specific number?

I want to keep 1,2,6,7,9,10, they will not move
I have this array
b=[1,5,2,8,6,11,7,9,3,10]
P=perms(b);
HOw to use perms for 3,5,8,11 among 6 numbers above?

 채택된 답변

Stephen23
Stephen23 2019년 5월 29일

0 개 추천

>> b = [1,5,2,8,6,11,7,9,3,10];
>> idx = ~ismember(b,[1,2,6,7,9,10]);
>> mat = repmat(b,factorial(nnz(idx)),1);
>> mat(:,idx) = perms(b(idx))
mat =
1 11 2 8 6 5 7 9 3 10
1 11 2 8 6 3 7 9 5 10
1 11 2 5 6 8 7 9 3 10
1 11 2 5 6 3 7 9 8 10
1 11 2 3 6 8 7 9 5 10
1 11 2 3 6 5 7 9 8 10
1 8 2 11 6 5 7 9 3 10
1 8 2 11 6 3 7 9 5 10
1 8 2 5 6 11 7 9 3 10
1 8 2 5 6 3 7 9 11 10
1 8 2 3 6 11 7 9 5 10
1 8 2 3 6 5 7 9 11 10
1 5 2 11 6 8 7 9 3 10
1 5 2 11 6 3 7 9 8 10
1 5 2 8 6 11 7 9 3 10
1 5 2 8 6 3 7 9 11 10
1 5 2 3 6 11 7 9 8 10
1 5 2 3 6 8 7 9 11 10
1 3 2 11 6 8 7 9 5 10
1 3 2 11 6 5 7 9 8 10
1 3 2 8 6 11 7 9 5 10
1 3 2 8 6 5 7 9 11 10
1 3 2 5 6 11 7 9 8 10
1 3 2 5 6 8 7 9 11 10

댓글 수: 6

Thank you! it's not enough actually, for example, between 1,2 may have all moving numbers
b = [1,5,2,8,6,11,7,9,3,10];
b = [1,5,3,2,8,6,11,7,9,10];
b = [1,5,2,8,6,11,7,3,9,10];
Stephen23
Stephen23 2019년 5월 29일
편집: Stephen23 2019년 5월 29일
How many "moving values" should appear together?:
  • minimum zero or one
  • maximum two or three or four or no limit
Do you want all permutations (24) as well as all possible combinations of moving different numbers of those values into the "moving" locations ?
Hang Vu
Hang Vu 2019년 5월 29일
편집: Hang Vu 2019년 5월 29일
3,5,8,11 are moving numbers, and all keep number should follow the order 1,2,6,7,9,10. Yes I want all possible of moving numbers but inside the keeping numbers. Like the example below, between 9,10 is no number. so it will be the perm of moving bnumbers and the space between keeping numbers I think
b = [1,5,3,2,8,6,11,7,9,10];
Hang Vu
Hang Vu 2019년 5월 29일
To make it easy, I think we can delete 1 and 10. only perm [5,3,2,8,6,11,7,9], since 1, 10 will be stable.
Stephen23
Stephen23 2019년 5월 29일
편집: Stephen23 2019년 5월 29일
Here is a simple but not-very-efficient solution (warning: generates all permutations and discards invalid ones) (note that 1 and 10 have been ignored to make the code simpler):
b = [5,2,8,6,11,7,9,3];
V = [2,6,7,9]; % fixed
P = perms(b);
[X,Y] = ismember(P.',V);
R = reshape(Y(X),[],size(P,1));
D = all(diff(R,1,1)>0);
Z = P(D,:)
Giving:
Z =
3 11 8 2 6 7 9 5
3 11 8 2 6 7 5 9
3 11 8 2 6 5 7 9
3 11 8 2 5 6 7 9
3 11 8 5 2 6 7 9
3 11 2 6 8 7 9 5
3 11 2 6 8 7 5 9
3 11 2 6 8 5 7 9
3 11 2 6 7 8 9 5
... lots of lines here
5 3 2 11 6 7 8 9
5 3 2 11 6 7 9 8
5 3 2 11 8 6 7 9
5 3 2 6 11 8 7 9
5 3 2 6 11 7 8 9
5 3 2 6 11 7 9 8
5 3 2 6 8 11 7 9
5 3 2 6 8 7 11 9
5 3 2 6 8 7 9 11
5 3 2 6 7 8 11 9
5 3 2 6 7 8 9 11
5 3 2 6 7 11 8 9
5 3 2 6 7 11 9 8
5 3 2 6 7 9 11 8
5 3 2 6 7 9 8 11
5 3 2 8 6 11 7 9
5 3 2 8 6 7 11 9
5 3 2 8 6 7 9 11
5 3 2 8 11 6 7 9
Hang Vu
Hang Vu 2019년 5월 29일
Thank you so much! this is what I want!

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

추가 답변 (1개)

Jan
Jan 2019년 5월 28일
편집: Jan 2019년 5월 28일

1 개 추천

You did not mention how the output should look like. Perhaps:
b = [1,5,2,8,6,11,7,9,3,10];
keep = [1,2,6,7,9,10];
move = ~ismember(b, keep);
P = perms(b(move));
R = repmat(b, size(P,1), 1);
R(:, move) = P

댓글 수: 6

Hang Vu
Hang Vu 2019년 5월 29일
편집: Hang Vu 2019년 5월 29일
Thank you Jan! I am sorry, I did not make it more clear. The output may look like b. because I want to list out all posibility of moving for 3, 5, 8, 11 by using perms, and 1, 10 should be in the border and 1,2,6,7,9,10 should follow the order also:
b = [1,5,2,8,6,11,7,9,3,10];
b = [1,5,3,2,8,6,11,7,9,10];
b = [1,5,2,8,6,11,7,3,9,10];
Stephen23
Stephen23 2019년 5월 29일
@Hang Vu: your examples contradict what you wrote in your question: "I want to keep 1,2,6,7,9,10, they will not move". Clearly in your examples they have moved. Confusing.
Jan
Jan 2019년 5월 29일
@Hang Vu: If I assume, that your examples contain some typos, the output R contains all wanted vectors as rows. So is your problem solved?
Hang Vu
Hang Vu 2019년 5월 29일
Thank you @Jan, I solved my problem. This one is another problem
@Stephen: I mean the order is not changed. if the moving numbers move then you will see the keeping move...
Jan
Jan 2019년 5월 29일
@Hang Vu: I do not understand, what you mean. You solved the problem, but this is another one? Is the problem of your question solved or not?
"if the moving numbers move then you will see the keeping move..." - what does this mean?
Please explain exactly, what you want to achieve.
Hang Vu
Hang Vu 2019년 5월 29일
I am sorry, I miss-understood. I want to list all possible array of the moving numbers between the keeping one

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2019년 5월 28일

댓글:

2019년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by