3
$\begingroup$

I am on permutation project, and I have trouble with my result, the sequence of my result is not same with Permutations in Mathematica here the code

x[l_, elmn_] :=   Join[{h[l, elmn][[elmn - 2]]}, 
Reverse[ {h[l, elmn][[elmn - 1]] , h[l, elmn] [[elmn]]}]];

Table[{h[l, 3], x[l, 3]}, {l, 3}]

{{{2, 3, 1}, {2, 1, 3}}, {{3, 1, 2}, {3, 2, 1}}, {{1, 2, 3}, {1, 3, 2}}}

But the result on Mathematica Permtations is

Permutations[{1, 2, 3}]

{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}

I want to sort it to make it exactly same like Permutations

$\endgroup$
  • 1
    $\begingroup$ Is the intended question here that you want to Sort the lists at the bottom level in the input array? You say "short", but the result shown looks sorted. If that's so, do you want to preserve the structure of the original array? $\endgroup$ – kjosborne Apr 30 '18 at 20:34
  • $\begingroup$ yes, i want the matriks array still on their position, like Permutations[{1,2,3,4,5}]. i am already have the matriks. but their posistion its so random $\endgroup$ – Bagoes Heikhal Apr 30 '18 at 20:40
  • 1
    $\begingroup$ Can you check the brackets in the first line of code? With list = {{2, 3, 4, 5, 1}, {2, 1, 5, 4, 3}, {3, 2, 1, 5, 4}}, Sort /@ list does what I understand you want. $\endgroup$ – anderstood Apr 30 '18 at 21:14
  • $\begingroup$ That structure also more closely matches what we would expect Permutations[{1, 2, 3, 4, 5}] to look like, and that was mentioned in the question. $\endgroup$ – kjosborne Apr 30 '18 at 21:28
1
$\begingroup$

Here is how you can change the answer you got into the same format as the output of Permutations. Start with your list, flatten, and sort:

list = {{{2, 3, 1}, {2, 1, 3}}, {{3, 1, 2}, {3, 2, 1}}, {{1, 2, 3}, {1, 3, 2}}};
Sort[Flatten[list, 1]]

{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.