m={{a, b, c, p}, {d, e, f, x}, {g, h, k, z}, {u, v, w, y}};

I want to swap 1st and 2nd rows by 4th and 3rd rows respectively. And then, swap 1st and 2nd columns by 4th and 3rd columns.

  • 2
    $\begingroup$ Reverse[m, {1, 2}]. Related: (2546) and (19726) $\endgroup$ – Kuba Apr 29 '14 at 15:52
  • $\begingroup$ I don't think it is obvious that Reverse has second argument, but if you need to reverse something it is easy to face it in docs :) So my point is that it should be closed, but let's see what community says :) $\endgroup$ – Kuba Apr 29 '14 at 16:03
  • 2
    $\begingroup$ @Kuba Unless it's a dup (I don't know), I think you could post it as an answer $\endgroup$ – Dr. belisarius Apr 29 '14 at 16:07
  • $\begingroup$ Also related: (3069) and (20228) $\endgroup$ – Mr.Wizard Apr 29 '14 at 23:33

OK, encouraged by belisarius, here's the way.

If you know that Reverse exists you may use:

Reverse /@ Reverse @ m

then you could check the docs and realise that Reverse has 2nd argument:

Reverse[m, {1, 2}]

But if you don't but you are amazed by Span+Part you can end up with:

m[[;; , {4, 3, 2, 1}]][[{4, 3, 2, 1}]]

or even more Span: :)

m[[;; , -1 ;; 1 ;; -1]][[-1 ;; 1 ;; -1]]

Why is multidimensional Reverse slow? - nice question where I've learned about -1;;1;;-1

  • 1
    $\begingroup$ Congratulations on joining the 20K club! :-) For some reason I still think of you as a "new" member but you have become a pillar of the community, and not by "reputation" alone. $\endgroup$ – Mr.Wizard Apr 29 '14 at 23:35
  • $\begingroup$ @Mr.Wizard Thank you for those kind words :) I do enjoy being here :), almost each day I can learn, or at least face, something new so thank you community of Mathematica.SE :) $\endgroup$ – Kuba Apr 30 '14 at 5:12

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.