2
$\begingroup$

I have a matrix:

$m=\begin{bmatrix} a & b & c & d\\ e & f & g & h\\ i & j & k & l\\ m & n & o & p\\ q & r & s & t\\ u & v & w & x \end{bmatrix}$

and I want to sort the rows of the matrix using a sort list:

$sortVec=\left\{ \left\{ 2,1,3\},\{3,1,2\}\right\} \right\}$

so that the output is:

$m=\begin{bmatrix} e & f & g & h\\ a & b & c & d\\ i & j & k & l\\ q & r & s & t\\ u & v & w & x\\ m & n & o & p \end{bmatrix}$

The problem is that when I want to use something like:

Permute[data, sortVec]

then sortVec would need to be

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

but I don't want to type out the entire vector, but rather permute parts of the matrix...

$\endgroup$
4
  • $\begingroup$ Your second matrix doesn't match up with your $sortVec$. If it did, then this would work: m[[Catenate[sortVec + 3 Range[0, Length@sortVec - 1]]]] $\endgroup$
    – Jason B.
    Commented Aug 9, 2016 at 16:30
  • 2
    $\begingroup$ Possible duplicate of Sort rows in a matrix with a vector $\endgroup$
    – Jason B.
    Commented Aug 9, 2016 at 16:30
  • $\begingroup$ @JasonB, please look at my edit. $\endgroup$
    – henry
    Commented Aug 9, 2016 at 16:36
  • $\begingroup$ People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Commented Aug 9, 2016 at 17:31

1 Answer 1

2
$\begingroup$

I think this does what you want:

m = Partition[Alphabet[], 4];

sortVec = {{2, 1, 3}, {3, 1, 2}};

Permute[m, Join @@ FoldList[Max[#] + #2 &, sortVec]]

$\left( \begin{array}{cccc} \text{e} & \text{f} & \text{g} & \text{h} \\ \text{a} & \text{b} & \text{c} & \text{d} \\ \text{i} & \text{j} & \text{k} & \text{l} \\ \text{q} & \text{r} & \text{s} & \text{t} \\ \text{u} & \text{v} & \text{w} & \text{x} \\ \text{m} & \text{n} & \text{o} & \text{p} \\ \end{array} \right)$

It also works with mixed cycle lengths:

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

Join @@ FoldList[Max[#] + #2 &, %]

Permute[m, %]
{2, 1, 5, 3, 4, 6}

$\left( \begin{array}{cccc} \text{e} & \text{f} & \text{g} & \text{h} \\ \text{a} & \text{b} & \text{c} & \text{d} \\ \text{m} & \text{n} & \text{o} & \text{p} \\ \text{q} & \text{r} & \text{s} & \text{t} \\ \text{i} & \text{j} & \text{k} & \text{l} \\ \text{u} & \text{v} & \text{w} & \text{x} \\ \end{array} \right)$

$\endgroup$
6
  • $\begingroup$ Wizard Great ! Thank you very much! $\endgroup$
    – henry
    Commented Aug 9, 2016 at 16:47
  • $\begingroup$ @DoHe You're welcome. However I see a possible problem; is your sortVec always composed of lists of the same length, or could you have something like sortVec = {{2, 1}, {3, 1, 2}, {1}}? If the latter we will need something more general. $\endgroup$
    – Mr.Wizard
    Commented Aug 9, 2016 at 16:49
  • $\begingroup$ hmm you are right, it would probably need something more general. $\endgroup$
    – henry
    Commented Aug 9, 2016 at 16:53
  • $\begingroup$ @DoHe Please see the update. $\endgroup$
    – Mr.Wizard
    Commented Aug 9, 2016 at 16:54
  • $\begingroup$ Wizard: Great work !! Thank you! $\endgroup$
    – henry
    Commented Aug 9, 2016 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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