# How to interchange columns of a matrix? [duplicate]

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 !

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

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.

• Your Map hanges the rows not the columns I think! – Ulrich Neumann May 29 '19 at 11:14
• It changes each row separately to the requested format. Try it, it gives the correct form. – a20 May 29 '19 at 11:15
• You're right, but in the question I read " interchange columns" – Ulrich Neumann May 29 '19 at 11:17
• @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. – Chetan Waghela May 29 '19 at 11:21
• N is a protected symbol in Mathematica, you cannot assign values to it. – Roman May 29 '19 at 11:59