Are there any good mass row/column swapping functions for matrices? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T06:13:56Z https://mathematica.stackexchange.com/feeds/question/80074 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/80074 2 Are there any good mass row/column swapping functions for matrices? John Yates https://mathematica.stackexchange.com/users/27807 2015-04-16T11:41:54Z 2015-04-16T13:38:06Z <p>I have the following matrix</p> <p>Keeping the 20 row and 20 column fixed (so the 21st rows and columns because I started at 0)...how do I push each row and column back one spot?</p> <p>I need to push the 0th row and column to the 19th row and column, the 1st row and column to the 0th row and column..so pretty much swapping out every row and column with the one before it while keeping the 20th row and column fixed. </p> <p>$\begin{bmatrix}a_{0,0} &amp;a_{0,1}&amp;....&amp;a_{0,20}\\.&amp;.&amp;&amp;.\\.&amp;.&amp;&amp;.\\.&amp;.&amp;&amp;.\\a_{20,0} &amp; a_{20,1}&amp;....&amp;a_{20,20}\end{bmatrix}$ </p> <p><img src="https://i.stack.imgur.com/zHHGU.png" alt="code"></p> https://mathematica.stackexchange.com/questions/80074/-/80075#80075 4 Answer by Kuba for Are there any good mass row/column swapping functions for matrices? Kuba https://mathematica.stackexchange.com/users/5478 2015-04-16T11:52:04Z 2015-04-16T13:38:06Z <p>Small example on 5x5 matrix:</p> <pre><code>pp = Table[p[i, j], {i, 5}, {j, 5}] </code></pre> <p><img src="https://i.stack.imgur.com/uQxGD.png" alt="enter image description here"></p> <hr> <p>One way:</p> <pre><code>pp[[#, #]] &amp;@Insert[Rest@Range, 1, -2] </code></pre> <p><img src="https://i.stack.imgur.com/LrA8N.png" alt="enter image description here"></p> <hr> <p>Or another:</p> <pre><code>pp[[;; 4, ;; 4]] = RotateLeft[pp[[;; 4, ;; 4]], {1, 1}]; </code></pre> https://mathematica.stackexchange.com/questions/80074/-/80079#80079 2 Answer by LLlAMnYP for Are there any good mass row/column swapping functions for matrices? LLlAMnYP https://mathematica.stackexchange.com/users/26956 2015-04-16T13:32:24Z 2015-04-16T13:32:24Z <p>I'm a bit late coming in here with this, but let there be a different approach anyway. It so happens, that the permutation you describe is simply <code>Cycles[{Range@20}]</code>:</p> <pre><code>Permute[Range@21,Cycles[{Range@20}]] (* {20, 1, 2, ..., 19, 21} *) </code></pre> <p>With <code>mat = Table[p[i,j],{i,0,20},{j,0,20}]</code> You can try this:</p> <pre><code>Transpose@ MapAt[Permute[#, Cycles[{Range@20}]] &amp;, Transpose@MapAt[Permute[#, Cycles[{Range@20}]] &amp;, mat, {;; 20}], {;; 20}] </code></pre> <p>This will return you the complete desired output without having to modify parts of the matrix step-by-step.</p> <p>Note to self and question to audience: because I have to permute first rows, then columns, I needed to make transpositions. It would be great if there's a way to have <code>MapAt</code> act on an entire column, similar to how here it acted on rows.</p>