1

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 !

Chetan Waghela
  • 229
  • 2
  • 7

1 Answers1

0

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

a20
  • 912
  • 5
  • 17