0
$\begingroup$

I have a table-1 having 36 elements, each having the form {A, m, n}, where m & n are the indices (1-6).

There is another table-2 having 36 elements {m, n} in a particular order (like {{4, 3}, {2, 3}....} etc.).

I want to arrange the elements of first table in a way that it follows the same order of m & n as in table 2. How can I achieve it. Thanks

$\endgroup$
  • 1
    $\begingroup$ I would look at the Ordering function -- you should be able to get something like Ordering[table2[[All, 2;;3]]] then apply that ordering to table 1. link $\endgroup$ – nben Apr 12 '18 at 18:54
2
$\begingroup$

Fake data:

table1 = Prepend[#, A] & /@ RandomInteger[{1, 6}, {10, 2}]
table2 = Tuples[Range[6], 2]

Sort table1 by table2 positions:

SortBy[table1, Position[table2, #[[2 ;; 3]]] &]
$\endgroup$
2
$\begingroup$

Fleshing out @user16054's suggestion, here is an example table 1 and an example target:

SeedRandom[1];
table1 = Flatten /@ Thread[{
    Array[a, 36],
    Tuples[Range[6], 2][[PermutationList @ RandomPermutation @ SymmetricGroup[36]]]
}]

target = Tuples[Range[6], 2][[PermutationList @ RandomPermutation @ SymmetricGroup[36]]]

{{a[1], 4, 3}, {a[2], 1, 2}, {a[3], 1, 3}, {a[4], 2, 1}, {a[5], 6, 5}, {a[6], 2, 5}, {a[7], 6, 2}, {a[8], 5, 2}, {a[9], 5, 6}, {a[10], 3, 4}, {a[11], 2, 2}, {a[12], 5, 1}, {a[13], 3, 6}, {a[14], 6, 3}, {a[15], 2, 6}, {a[16], 1, 6}, {a[17], 5, 4}, {a[18], 4, 2}, {a[19], 4, 5}, {a[20], 2, 4}, {a[21], 4, 6}, {a[22], 6, 1}, {a[23], 3, 5}, {a[24], 3, 1}, {a[25], 5, 3}, {a[26], 1, 1}, {a[27], 6, 4}, {a[28], 5, 5}, {a[29], 6, 6}, {a[30], 4, 4}, {a[31], 2, 3}, {a[32], 4, 1}, {a[33], 3, 2}, {a[34], 3, 3}, {a[35], 1, 4}, {a[36], 1, 5}}

{{3, 1}, {3, 4}, {5, 5}, {4, 4}, {1, 3}, {4, 6}, {1, 2}, {6, 5}, {2, 2}, {1, 5}, {2, 3}, {3, 2}, {5, 2}, {3, 5}, {2, 5}, {1, 6}, {5, 1}, {3, 6}, {2, 6}, {6, 1}, {4, 3}, {2, 1}, {6, 4}, {3, 3}, {4, 1}, {6, 3}, {5, 3}, {1, 4}, {1, 1}, {4, 5}, {5, 6}, {6, 6}, {5, 4}, {2, 4}, {6, 2}, {4, 2}}

Then, determine the ordering of table1 and target:

ord1 = Ordering[table1[[All, 2;;]]]
ordt = Ordering[target]

{26, 2, 3, 35, 36, 16, 4, 11, 31, 20, 6, 15, 24, 33, 34, 10, 23, 13, 32, 18, 1, 30, 19, 21, 12, 8, 25, 17, 28, 9, 22, 7, 14, 27, 5, 29}

{29, 7, 5, 28, 10, 16, 22, 9, 11, 34, 15, 19, 1, 12, 24, 2, 14, 18, 25, 36, 21, 4, 30, 6, 17, 13, 27, 33, 3, 31, 20, 35, 26, 23, 8, 32}

Finally, invert ordt and use the inverse and ord1 to reorder table 1:

res = table1[[ord1[[InversePermutation @ ordt]]]]

{{a[24], 3, 1}, {a[10], 3, 4}, {a[28], 5, 5}, {a[30], 4, 4}, {a[3], 1, 3}, {a[21], 4, 6}, {a[2], 1, 2}, {a[5], 6, 5}, {a[11], 2, 2}, {a[36], 1, 5}, {a[31], 2, 3}, {a[33], 3, 2}, {a[8], 5, 2}, {a[23], 3, 5}, {a[6], 2, 5}, {a[16], 1, 6}, {a[12], 5, 1}, {a[13], 3, 6}, {a[15], 2, 6}, {a[22], 6, 1}, {a[1], 4, 3}, {a[4], 2, 1}, {a[27], 6, 4}, {a[34], 3, 3}, {a[32], 4, 1}, {a[14], 6, 3}, {a[25], 5, 3}, {a[35], 1, 4}, {a[26], 1, 1}, {a[19], 4, 5}, {a[9], 5, 6}, {a[29], 6, 6}, {a[17], 5, 4}, {a[20], 2, 4}, {a[7], 6, 2}, {a[18], 4, 2}}

The output above is ordered in the same way as target.

$\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.