1
$\begingroup$

This question already has an answer here:

Suppose I have a matrix

M={{1,2,3},{4,5,6},{7,8,9}}//MatrixForm

What code do I write to interchange columns and make it into

{{2,3,1},{5,6,4},{8,9,7}}

The question mentioned here, Elegant operations on matrix rows and columns

does not solve the problem because, I am using //MatrixForm with the list, and it is not working for interchanging columns, while it works for interchanging rows !

$\endgroup$

marked as duplicate by Michael E2, m_goldberg, Henrik Schumacher, MarcoB, user42582 Jun 3 at 13:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ RotateLeft /@ M $\endgroup$ – J42161217 May 29 at 11:08
  • 1
    $\begingroup$ Permute[#,Cycles[{{3,2,1}}]]& /@ M $\endgroup$ – amator2357 May 29 at 11:10
  • 3
    $\begingroup$ M[[All, {2, 3, 1}]]? $\endgroup$ – kglr May 29 at 11:15
  • $\begingroup$ @kglr it interchanges rows not columns. Program is not working for columns !!! $\endgroup$ – Chetan Waghela May 29 at 11:18
  • 4
    $\begingroup$ Yeah, MatrixForm does not work like some other "Forms": mathematica.stackexchange.com/questions/3098/… $\endgroup$ – Michael E2 May 29 at 11:30
0
$\begingroup$

There are several methods, but the quickest, general one that is easily adjusted for more complicated permutations, is probably:

M = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Map[{#[[2]],#[[3]],#[[1]]}&,M]

That is, you take every row of M, and create the list {#[[2]],#[[3]],#[1]}, where e.g. #[[2]] refers to the second element of the row that is being acted upon.

You can of course also solve this by simple matrix multiplication:

A = {{0,0,1},{1,0,0},{0,1,0}}

M.A then gives you your requested matrix.

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Your Map hanges the rows not the columns I think! $\endgroup$ – Ulrich Neumann May 29 at 11:14
  • $\begingroup$ It changes each row separately to the requested format. Try it, it gives the correct form. $\endgroup$ – bjorn May 29 at 11:15
  • $\begingroup$ You're right, but in the question I read " interchange columns" $\endgroup$ – Ulrich Neumann May 29 at 11:17
  • $\begingroup$ @Ulrich Neumann Yes you are right I am not able to interchange columns. I tried a lot of things, maybe I am doing something wrong. $\endgroup$ – Chetan Waghela May 29 at 11:21
  • 2
    $\begingroup$ N is a protected symbol in Mathematica, you cannot assign values to it. $\endgroup$ – Roman May 29 at 11:59

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