Changing the position of rows and columns in a matrix












3












$begingroup$


I have the following self-explanatory question.



https://1drv.ms/u/s!AsyHs3E_aioxhipb3wSPSX_heN-t



enter image description here



As seen from the above matrices, I start with the "original" matrix, which is a symmetric matrix, meaning that first row and first column represent the same variable, say X, and 2nd row and 2nd column represent the variable Y, following Z, W, V. I first want to move 2nd row in the "original" matrix to 4th row. This operation is shown in the matrix denoted by r24. After this operation, I want to do the same operation on the same columns, meaning that I want to move 2nd column to 4th column as shown in c24. All of these operations are shown with the colored text. The resulting final matrix, which I aim to create, c24, should be symmetric with respect to the variable names. It means that the final matrix has the ordered variable names as X, Z, W, Y, V in columns and rows. In fact, if the above two operations are done correctly, the order of the variables in rows and columns will remain identical.



I like to do all the operations using a Mathematica function such as f[original, 2, 4] to create the final matrix c24.



Thank you all.










share|improve this question











$endgroup$












  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    yesterday
















3












$begingroup$


I have the following self-explanatory question.



https://1drv.ms/u/s!AsyHs3E_aioxhipb3wSPSX_heN-t



enter image description here



As seen from the above matrices, I start with the "original" matrix, which is a symmetric matrix, meaning that first row and first column represent the same variable, say X, and 2nd row and 2nd column represent the variable Y, following Z, W, V. I first want to move 2nd row in the "original" matrix to 4th row. This operation is shown in the matrix denoted by r24. After this operation, I want to do the same operation on the same columns, meaning that I want to move 2nd column to 4th column as shown in c24. All of these operations are shown with the colored text. The resulting final matrix, which I aim to create, c24, should be symmetric with respect to the variable names. It means that the final matrix has the ordered variable names as X, Z, W, Y, V in columns and rows. In fact, if the above two operations are done correctly, the order of the variables in rows and columns will remain identical.



I like to do all the operations using a Mathematica function such as f[original, 2, 4] to create the final matrix c24.



Thank you all.










share|improve this question











$endgroup$












  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    yesterday














3












3








3


1



$begingroup$


I have the following self-explanatory question.



https://1drv.ms/u/s!AsyHs3E_aioxhipb3wSPSX_heN-t



enter image description here



As seen from the above matrices, I start with the "original" matrix, which is a symmetric matrix, meaning that first row and first column represent the same variable, say X, and 2nd row and 2nd column represent the variable Y, following Z, W, V. I first want to move 2nd row in the "original" matrix to 4th row. This operation is shown in the matrix denoted by r24. After this operation, I want to do the same operation on the same columns, meaning that I want to move 2nd column to 4th column as shown in c24. All of these operations are shown with the colored text. The resulting final matrix, which I aim to create, c24, should be symmetric with respect to the variable names. It means that the final matrix has the ordered variable names as X, Z, W, Y, V in columns and rows. In fact, if the above two operations are done correctly, the order of the variables in rows and columns will remain identical.



I like to do all the operations using a Mathematica function such as f[original, 2, 4] to create the final matrix c24.



Thank you all.










share|improve this question











$endgroup$




I have the following self-explanatory question.



https://1drv.ms/u/s!AsyHs3E_aioxhipb3wSPSX_heN-t



enter image description here



As seen from the above matrices, I start with the "original" matrix, which is a symmetric matrix, meaning that first row and first column represent the same variable, say X, and 2nd row and 2nd column represent the variable Y, following Z, W, V. I first want to move 2nd row in the "original" matrix to 4th row. This operation is shown in the matrix denoted by r24. After this operation, I want to do the same operation on the same columns, meaning that I want to move 2nd column to 4th column as shown in c24. All of these operations are shown with the colored text. The resulting final matrix, which I aim to create, c24, should be symmetric with respect to the variable names. It means that the final matrix has the ordered variable names as X, Z, W, Y, V in columns and rows. In fact, if the above two operations are done correctly, the order of the variables in rows and columns will remain identical.



I like to do all the operations using a Mathematica function such as f[original, 2, 4] to create the final matrix c24.



Thank you all.







matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









user64494

3,55311022




3,55311022










asked yesterday









Tugrul TemelTugrul Temel

865213




865213












  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    yesterday


















  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    yesterday
















$begingroup$
closely related/ possible duplicate: Move element inside a list
$endgroup$
– kglr
yesterday




$begingroup$
closely related/ possible duplicate: Move element inside a list
$endgroup$
– kglr
yesterday










4 Answers
4






active

oldest

votes


















4












$begingroup$

Simply use Part and Set:



f[A_?SquareMatrixQ, i_Integer, j_Integer] := Module[{p, idx},
idx = Range[i, j, Sign[j - i]];
p = Range[1, Length[A]];
p[[idx]] = RotateLeft[idx];
A[[p, p]]
]

A = Outer[Plus, Range[5], Range[0, 4]];
A // MatrixForm



$left(
begin{array}{ccccc}
1 & 2 & 3 & 4 & 5 \
2 & 3 & 4 & 5 & 6 \
3 & 4 & 5 & 6 & 7 \
4 & 5 & 6 & 7 & 8 \
5 & 6 & 7 & 8 & 9 \
end{array}
right)$




B = f[A,2,4];
B // MatrixForm



$left(
begin{array}{ccccc}
1 & 3 & 4 & 2 & 5 \
3 & 5 & 6 & 4 & 7 \
4 & 6 & 7 & 5 & 8 \
2 & 4 & 5 & 3 & 6 \
5 & 7 & 8 & 6 & 9 \
end{array}
right)$







share|improve this answer











$endgroup$













  • $begingroup$
    Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
    $endgroup$
    – Tugrul Temel
    yesterday










  • $begingroup$
    @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
    $endgroup$
    – Henrik Schumacher
    yesterday










  • $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    yesterday






  • 1




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    6 hours ago






  • 2




    $begingroup$
    @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
    $endgroup$
    – Henrik Schumacher
    6 hours ago





















4












$begingroup$

to move a row from $i$ to $j$:



rowmove[A_?MatrixQ, i_Integer, j_Integer] := Insert[Delete[A, i], A[[i]], j]


to move a column from $i$ to $j$:



colmove[A_?MatrixQ, i_Integer, j_Integer] := Transpose@rowmove[Transpose[A], i, j]


both at the same time:



move[A_?MatrixQ, i_Integer, j_Integer] := colmove[rowmove[A, i, j], i, j]





share|improve this answer









$endgroup$













  • $begingroup$
    Clean solution, copying it...
    $endgroup$
    – MikeY
    yesterday










  • $begingroup$
    Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
    $endgroup$
    – Okkes Dulgerci
    16 hours ago










  • $begingroup$
    @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
    $endgroup$
    – Roman
    15 hours ago





















3












$begingroup$

Define the indices that you want to interchange in a list $ind$ and then you can index into the array $a$ directly.



a = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7},
{4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}};
ind = {1, 3, 4, 2, 5};
a[[ind, ind]]


If you don't want to specify the index array each time, Roman points out that you can build a simple function to do it:



indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]


So to get the above you would specify



ind = indexlist[5,2,4]





share|improve this answer











$endgroup$













  • $begingroup$
    You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
    $endgroup$
    – Roman
    yesterday



















0












$begingroup$

f[A_?MatrixQ, i_Integer, j_Integer] := With[{B = Transpose@Insert[Delete[A, i], A[[i]], j]}, 
Transpose@Insert[Delete[B, i], B[[i]], j]]
f[A, 2, 4] // MatrixForm





share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193640%2fchanging-the-position-of-rows-and-columns-in-a-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4












    $begingroup$

    Simply use Part and Set:



    f[A_?SquareMatrixQ, i_Integer, j_Integer] := Module[{p, idx},
    idx = Range[i, j, Sign[j - i]];
    p = Range[1, Length[A]];
    p[[idx]] = RotateLeft[idx];
    A[[p, p]]
    ]

    A = Outer[Plus, Range[5], Range[0, 4]];
    A // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 2 & 3 & 4 & 5 \
    2 & 3 & 4 & 5 & 6 \
    3 & 4 & 5 & 6 & 7 \
    4 & 5 & 6 & 7 & 8 \
    5 & 6 & 7 & 8 & 9 \
    end{array}
    right)$




    B = f[A,2,4];
    B // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 3 & 4 & 2 & 5 \
    3 & 5 & 6 & 4 & 7 \
    4 & 6 & 7 & 5 & 8 \
    2 & 4 & 5 & 3 & 6 \
    5 & 7 & 8 & 6 & 9 \
    end{array}
    right)$







    share|improve this answer











    $endgroup$













    • $begingroup$
      Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
      $endgroup$
      – Tugrul Temel
      yesterday










    • $begingroup$
      @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
      $endgroup$
      – Henrik Schumacher
      yesterday










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      yesterday






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      6 hours ago






    • 2




      $begingroup$
      @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
      $endgroup$
      – Henrik Schumacher
      6 hours ago


















    4












    $begingroup$

    Simply use Part and Set:



    f[A_?SquareMatrixQ, i_Integer, j_Integer] := Module[{p, idx},
    idx = Range[i, j, Sign[j - i]];
    p = Range[1, Length[A]];
    p[[idx]] = RotateLeft[idx];
    A[[p, p]]
    ]

    A = Outer[Plus, Range[5], Range[0, 4]];
    A // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 2 & 3 & 4 & 5 \
    2 & 3 & 4 & 5 & 6 \
    3 & 4 & 5 & 6 & 7 \
    4 & 5 & 6 & 7 & 8 \
    5 & 6 & 7 & 8 & 9 \
    end{array}
    right)$




    B = f[A,2,4];
    B // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 3 & 4 & 2 & 5 \
    3 & 5 & 6 & 4 & 7 \
    4 & 6 & 7 & 5 & 8 \
    2 & 4 & 5 & 3 & 6 \
    5 & 7 & 8 & 6 & 9 \
    end{array}
    right)$







    share|improve this answer











    $endgroup$













    • $begingroup$
      Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
      $endgroup$
      – Tugrul Temel
      yesterday










    • $begingroup$
      @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
      $endgroup$
      – Henrik Schumacher
      yesterday










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      yesterday






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      6 hours ago






    • 2




      $begingroup$
      @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
      $endgroup$
      – Henrik Schumacher
      6 hours ago
















    4












    4








    4





    $begingroup$

    Simply use Part and Set:



    f[A_?SquareMatrixQ, i_Integer, j_Integer] := Module[{p, idx},
    idx = Range[i, j, Sign[j - i]];
    p = Range[1, Length[A]];
    p[[idx]] = RotateLeft[idx];
    A[[p, p]]
    ]

    A = Outer[Plus, Range[5], Range[0, 4]];
    A // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 2 & 3 & 4 & 5 \
    2 & 3 & 4 & 5 & 6 \
    3 & 4 & 5 & 6 & 7 \
    4 & 5 & 6 & 7 & 8 \
    5 & 6 & 7 & 8 & 9 \
    end{array}
    right)$




    B = f[A,2,4];
    B // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 3 & 4 & 2 & 5 \
    3 & 5 & 6 & 4 & 7 \
    4 & 6 & 7 & 5 & 8 \
    2 & 4 & 5 & 3 & 6 \
    5 & 7 & 8 & 6 & 9 \
    end{array}
    right)$







    share|improve this answer











    $endgroup$



    Simply use Part and Set:



    f[A_?SquareMatrixQ, i_Integer, j_Integer] := Module[{p, idx},
    idx = Range[i, j, Sign[j - i]];
    p = Range[1, Length[A]];
    p[[idx]] = RotateLeft[idx];
    A[[p, p]]
    ]

    A = Outer[Plus, Range[5], Range[0, 4]];
    A // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 2 & 3 & 4 & 5 \
    2 & 3 & 4 & 5 & 6 \
    3 & 4 & 5 & 6 & 7 \
    4 & 5 & 6 & 7 & 8 \
    5 & 6 & 7 & 8 & 9 \
    end{array}
    right)$




    B = f[A,2,4];
    B // MatrixForm



    $left(
    begin{array}{ccccc}
    1 & 3 & 4 & 2 & 5 \
    3 & 5 & 6 & 4 & 7 \
    4 & 6 & 7 & 5 & 8 \
    2 & 4 & 5 & 3 & 6 \
    5 & 7 & 8 & 6 & 9 \
    end{array}
    right)$








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 6 hours ago

























    answered yesterday









    Henrik SchumacherHenrik Schumacher

    57.4k578158




    57.4k578158












    • $begingroup$
      Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
      $endgroup$
      – Tugrul Temel
      yesterday










    • $begingroup$
      @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
      $endgroup$
      – Henrik Schumacher
      yesterday










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      yesterday






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      6 hours ago






    • 2




      $begingroup$
      @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
      $endgroup$
      – Henrik Schumacher
      6 hours ago




















    • $begingroup$
      Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
      $endgroup$
      – Tugrul Temel
      yesterday










    • $begingroup$
      @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
      $endgroup$
      – Henrik Schumacher
      yesterday










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      yesterday






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      6 hours ago






    • 2




      $begingroup$
      @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
      $endgroup$
      – Henrik Schumacher
      6 hours ago


















    $begingroup$
    Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
    $endgroup$
    – Tugrul Temel
    yesterday




    $begingroup$
    Your answer is very good, with one slight change. When you move the 2nd row to 4th row, the order of the rows should remain unchanged. It means that when you move the 2nd row, the 3rd and 4th rows should just be moved up without changing the order. Of course the same applies to the column movements. This is important for my purpose and that is why I gave a name for rows and columns. Thank you very much for your very quick answer.
    $endgroup$
    – Tugrul Temel
    yesterday












    $begingroup$
    @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
    $endgroup$
    – Henrik Schumacher
    yesterday




    $begingroup$
    @TugrulTemel I see. Is this better now? I am not sure if the case $i>j$ is handled as you expect.
    $endgroup$
    – Henrik Schumacher
    yesterday












    $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    yesterday




    $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    yesterday




    1




    1




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    6 hours ago




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    6 hours ago




    2




    2




    $begingroup$
    @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
    $endgroup$
    – Henrik Schumacher
    6 hours ago






    $begingroup$
    @Mr.Wizard Sorry, I've been out tonight. This won't happen again, I promise ;) Thank you, that is indeed simpler. I took the freedom to incorporate that into my solution (in an updated version with more performance).
    $endgroup$
    – Henrik Schumacher
    6 hours ago













    4












    $begingroup$

    to move a row from $i$ to $j$:



    rowmove[A_?MatrixQ, i_Integer, j_Integer] := Insert[Delete[A, i], A[[i]], j]


    to move a column from $i$ to $j$:



    colmove[A_?MatrixQ, i_Integer, j_Integer] := Transpose@rowmove[Transpose[A], i, j]


    both at the same time:



    move[A_?MatrixQ, i_Integer, j_Integer] := colmove[rowmove[A, i, j], i, j]





    share|improve this answer









    $endgroup$













    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      yesterday










    • $begingroup$
      Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
      $endgroup$
      – Okkes Dulgerci
      16 hours ago










    • $begingroup$
      @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
      $endgroup$
      – Roman
      15 hours ago


















    4












    $begingroup$

    to move a row from $i$ to $j$:



    rowmove[A_?MatrixQ, i_Integer, j_Integer] := Insert[Delete[A, i], A[[i]], j]


    to move a column from $i$ to $j$:



    colmove[A_?MatrixQ, i_Integer, j_Integer] := Transpose@rowmove[Transpose[A], i, j]


    both at the same time:



    move[A_?MatrixQ, i_Integer, j_Integer] := colmove[rowmove[A, i, j], i, j]





    share|improve this answer









    $endgroup$













    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      yesterday










    • $begingroup$
      Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
      $endgroup$
      – Okkes Dulgerci
      16 hours ago










    • $begingroup$
      @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
      $endgroup$
      – Roman
      15 hours ago
















    4












    4








    4





    $begingroup$

    to move a row from $i$ to $j$:



    rowmove[A_?MatrixQ, i_Integer, j_Integer] := Insert[Delete[A, i], A[[i]], j]


    to move a column from $i$ to $j$:



    colmove[A_?MatrixQ, i_Integer, j_Integer] := Transpose@rowmove[Transpose[A], i, j]


    both at the same time:



    move[A_?MatrixQ, i_Integer, j_Integer] := colmove[rowmove[A, i, j], i, j]





    share|improve this answer









    $endgroup$



    to move a row from $i$ to $j$:



    rowmove[A_?MatrixQ, i_Integer, j_Integer] := Insert[Delete[A, i], A[[i]], j]


    to move a column from $i$ to $j$:



    colmove[A_?MatrixQ, i_Integer, j_Integer] := Transpose@rowmove[Transpose[A], i, j]


    both at the same time:



    move[A_?MatrixQ, i_Integer, j_Integer] := colmove[rowmove[A, i, j], i, j]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    RomanRoman

    3,340819




    3,340819












    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      yesterday










    • $begingroup$
      Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
      $endgroup$
      – Okkes Dulgerci
      16 hours ago










    • $begingroup$
      @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
      $endgroup$
      – Roman
      15 hours ago




















    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      yesterday










    • $begingroup$
      Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
      $endgroup$
      – Okkes Dulgerci
      16 hours ago










    • $begingroup$
      @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
      $endgroup$
      – Roman
      15 hours ago


















    $begingroup$
    Clean solution, copying it...
    $endgroup$
    – MikeY
    yesterday




    $begingroup$
    Clean solution, copying it...
    $endgroup$
    – MikeY
    yesterday












    $begingroup$
    Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
    $endgroup$
    – Okkes Dulgerci
    16 hours ago




    $begingroup$
    Here is somewhat simplified version of your solution f[A_?MatrixQ, i_Integer, j_Integer] := (B = Insert[Delete[A, i], A[[i]], j]; Insert[Delete[Transpose@B, i], B[[All, i]], j]) or this one g[A_?MatrixQ, i_Integer, j_Integer] := (B = Transpose@Insert[Delete[A, i], A[[i]], j]; Transpose@Insert[Delete[B, i], B[[i]], j])
    $endgroup$
    – Okkes Dulgerci
    16 hours ago












    $begingroup$
    @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
    $endgroup$
    – Roman
    15 hours ago






    $begingroup$
    @OkkesDulgerci I don't think it's a good idea to use global variables like B in a function definition. Instead, I'd use With or Module to make the variable local.
    $endgroup$
    – Roman
    15 hours ago













    3












    $begingroup$

    Define the indices that you want to interchange in a list $ind$ and then you can index into the array $a$ directly.



    a = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7},
    {4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}};
    ind = {1, 3, 4, 2, 5};
    a[[ind, ind]]


    If you don't want to specify the index array each time, Roman points out that you can build a simple function to do it:



    indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]


    So to get the above you would specify



    ind = indexlist[5,2,4]





    share|improve this answer











    $endgroup$













    • $begingroup$
      You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
      $endgroup$
      – Roman
      yesterday
















    3












    $begingroup$

    Define the indices that you want to interchange in a list $ind$ and then you can index into the array $a$ directly.



    a = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7},
    {4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}};
    ind = {1, 3, 4, 2, 5};
    a[[ind, ind]]


    If you don't want to specify the index array each time, Roman points out that you can build a simple function to do it:



    indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]


    So to get the above you would specify



    ind = indexlist[5,2,4]





    share|improve this answer











    $endgroup$













    • $begingroup$
      You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
      $endgroup$
      – Roman
      yesterday














    3












    3








    3





    $begingroup$

    Define the indices that you want to interchange in a list $ind$ and then you can index into the array $a$ directly.



    a = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7},
    {4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}};
    ind = {1, 3, 4, 2, 5};
    a[[ind, ind]]


    If you don't want to specify the index array each time, Roman points out that you can build a simple function to do it:



    indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]


    So to get the above you would specify



    ind = indexlist[5,2,4]





    share|improve this answer











    $endgroup$



    Define the indices that you want to interchange in a list $ind$ and then you can index into the array $a$ directly.



    a = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7},
    {4, 5, 6, 7, 8}, {5, 6, 7, 8, 9}};
    ind = {1, 3, 4, 2, 5};
    a[[ind, ind]]


    If you don't want to specify the index array each time, Roman points out that you can build a simple function to do it:



    indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]


    So to get the above you would specify



    ind = indexlist[5,2,4]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 10 hours ago

























    answered yesterday









    bill sbill s

    54.5k377156




    54.5k377156












    • $begingroup$
      You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
      $endgroup$
      – Roman
      yesterday


















    • $begingroup$
      You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
      $endgroup$
      – Roman
      yesterday
















    $begingroup$
    You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
    $endgroup$
    – Roman
    yesterday




    $begingroup$
    You can get the index list ind with indexlist[n_, i_, j_] := Insert[Delete[Range[n], i], i, j]. In this case, indexlist[5, 2, 4] will give {1, 3, 4, 2, 5}.
    $endgroup$
    – Roman
    yesterday











    0












    $begingroup$

    f[A_?MatrixQ, i_Integer, j_Integer] := With[{B = Transpose@Insert[Delete[A, i], A[[i]], j]}, 
    Transpose@Insert[Delete[B, i], B[[i]], j]]
    f[A, 2, 4] // MatrixForm





    share|improve this answer









    $endgroup$


















      0












      $begingroup$

      f[A_?MatrixQ, i_Integer, j_Integer] := With[{B = Transpose@Insert[Delete[A, i], A[[i]], j]}, 
      Transpose@Insert[Delete[B, i], B[[i]], j]]
      f[A, 2, 4] // MatrixForm





      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        f[A_?MatrixQ, i_Integer, j_Integer] := With[{B = Transpose@Insert[Delete[A, i], A[[i]], j]}, 
        Transpose@Insert[Delete[B, i], B[[i]], j]]
        f[A, 2, 4] // MatrixForm





        share|improve this answer









        $endgroup$



        f[A_?MatrixQ, i_Integer, j_Integer] := With[{B = Transpose@Insert[Delete[A, i], A[[i]], j]}, 
        Transpose@Insert[Delete[B, i], B[[i]], j]]
        f[A, 2, 4] // MatrixForm






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 14 hours ago









        Okkes DulgerciOkkes Dulgerci

        5,4141919




        5,4141919






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193640%2fchanging-the-position-of-rows-and-columns-in-a-matrix%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How did Captain America manage to do this?

            迪纳利

            南乌拉尔铁路局