How to Permute the array with specific number?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hang Vu
am 28 Mai 2019
Kommentiert: Hang Vu
am 29 Mai 2019
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?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 29 Mai 2019
>> 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 Kommentare
Stephen23
am 29 Mai 2019
Bearbeitet: Stephen23
am 29 Mai 2019
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
Weitere Antworten (1)
Jan
am 28 Mai 2019
Bearbeitet: Jan
am 28 Mai 2019
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 Kommentare
Jan
am 29 Mai 2019
@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.
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!