Changing the position of rows and columns in a matrixMove element inside a listElegant operations on matrix rows and columnsSort matrix by columns and rows without changing themHow to merge Datasets representing matrices with named rows/columns?Question about the matrix operationChanging the first row of a matrix; subsequent rows depend on the first rowChange entire column of matrix using its own elements for calculationsCreating a matrix and plotting its columns with legendsSort matrix rows and columns while keeping headingOpposite of Part in matrices?reducing matrix size by linear operations on rows and columns

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

How did Monica know how to operate Carol's "designer"?

Is there a word to describe the feeling of being transfixed out of horror?

Why does this part of the Space Shuttle launch pad seem to be floating in air?

Java - What do constructor type arguments mean when placed *before* the type?

Why does the compiler allow throws when the method will never throw the Exception

Lightning Web Components - Not available in app builder

Would it be legal for a US State to ban exports of a natural resource?

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

Can a Bard use an arcane focus?

Suggestion on Missing Index Creation

A known event to a history junkie

Calculating the number of days between 2 dates in Excel

Lifted its hind leg on or lifted its hind leg towards?

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

What would you call a finite collection of unordered objects that are not necessarily distinct?

Is there enough fresh water in the world to eradicate the drinking water crisis?

Why has "pence" been used in this sentence, not "pences"?

Could solar power be utilized and substitute coal in the 19th century?

What if somebody invests in my application?

My boss asked me to take a one-day class, then signs it up as a day off

Make "apt-get update" show the exact output as `apt update`

Teaching indefinite integrals

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?



Changing the position of rows and columns in a matrix


Move element inside a listElegant operations on matrix rows and columnsSort matrix by columns and rows without changing themHow to merge Datasets representing matrices with named rows/columns?Question about the matrix operationChanging the first row of a matrix; subsequent rows depend on the first rowChange entire column of matrix using its own elements for calculationsCreating a matrix and plotting its columns with legendsSort matrix rows and columns while keeping headingOpposite of Part in matrices?reducing matrix size by linear operations on rows and columns













4












$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
    Mar 20 at 17:21










  • $begingroup$
    FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
    $endgroup$
    – J. M. is slightly pensive
    2 days ago















4












$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
    Mar 20 at 17:21










  • $begingroup$
    FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
    $endgroup$
    – J. M. is slightly pensive
    2 days ago













4












4








4


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.







list-manipulation matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









J. M. is slightly pensive

98.5k10308466




98.5k10308466










asked Mar 20 at 13:33









Tugrul TemelTugrul Temel

870213




870213











  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    Mar 20 at 17:21










  • $begingroup$
    FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
    $endgroup$
    – J. M. is slightly pensive
    2 days ago
















  • $begingroup$
    closely related/ possible duplicate: Move element inside a list
    $endgroup$
    – kglr
    Mar 20 at 17:21










  • $begingroup$
    FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
    $endgroup$
    – J. M. is slightly pensive
    2 days ago















$begingroup$
closely related/ possible duplicate: Move element inside a list
$endgroup$
– kglr
Mar 20 at 17:21




$begingroup$
closely related/ possible duplicate: Move element inside a list
$endgroup$
– kglr
Mar 20 at 17:21












$begingroup$
FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
$endgroup$
– J. M. is slightly pensive
2 days ago




$begingroup$
FWIW: your matrix is called a Hankel matrix. HankelMatrix[Range[5], Range[5, 9]]
$endgroup$
– J. M. is slightly pensive
2 days ago










4 Answers
4






active

oldest

votes


















5












$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(
beginarrayccccc
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 \
endarray
right)$




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



$left(
beginarrayccccc
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 \
endarray
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
    Mar 20 at 14:03










  • $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
    Mar 20 at 14:16










  • $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    Mar 20 at 14:34






  • 1




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    Mar 21 at 23:20






  • 3




    $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
    Mar 21 at 23:35



















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
    Mar 20 at 14:54










  • $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
    Mar 21 at 13:40










  • $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
    Mar 21 at 14:58



















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
    Mar 21 at 1:23


















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









    5












    $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(
    beginarrayccccc
    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 \
    endarray
    right)$




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



    $left(
    beginarrayccccc
    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 \
    endarray
    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
      Mar 20 at 14:03










    • $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
      Mar 20 at 14:16










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      Mar 20 at 14:34






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      Mar 21 at 23:20






    • 3




      $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
      Mar 21 at 23:35
















    5












    $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(
    beginarrayccccc
    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 \
    endarray
    right)$




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



    $left(
    beginarrayccccc
    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 \
    endarray
    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
      Mar 20 at 14:03










    • $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
      Mar 20 at 14:16










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      Mar 20 at 14:34






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      Mar 21 at 23:20






    • 3




      $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
      Mar 21 at 23:35














    5












    5








    5





    $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(
    beginarrayccccc
    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 \
    endarray
    right)$




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



    $left(
    beginarrayccccc
    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 \
    endarray
    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(
    beginarrayccccc
    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 \
    endarray
    right)$




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



    $left(
    beginarrayccccc
    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 \
    endarray
    right)$








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 21 at 23:32

























    answered Mar 20 at 13:51









    Henrik SchumacherHenrik Schumacher

    57.7k579158




    57.7k579158











    • $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
      Mar 20 at 14:03










    • $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
      Mar 20 at 14:16










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      Mar 20 at 14:34






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      Mar 21 at 23:20






    • 3




      $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
      Mar 21 at 23:35

















    • $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
      Mar 20 at 14:03










    • $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
      Mar 20 at 14:16










    • $begingroup$
      Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
      $endgroup$
      – Tugrul Temel
      Mar 20 at 14:34






    • 1




      $begingroup$
      You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
      $endgroup$
      – Mr.Wizard
      Mar 21 at 23:20






    • 3




      $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
      Mar 21 at 23:35
















    $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
    Mar 20 at 14:03




    $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
    Mar 20 at 14:03












    $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
    Mar 20 at 14:16




    $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
    Mar 20 at 14:16












    $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    Mar 20 at 14:34




    $begingroup$
    Excellent...that is what I aim to achieve. Really, thank you so much for your prompt answer. regards, Tugrul
    $endgroup$
    – Tugrul Temel
    Mar 20 at 14:34




    1




    1




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    Mar 21 at 23:20




    $begingroup$
    You didn't reply, but anyway: q = Range[i, j, Sign[j - i]]; p = RotateLeft[q];
    $endgroup$
    – Mr.Wizard
    Mar 21 at 23:20




    3




    3




    $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
    Mar 21 at 23:35





    $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
    Mar 21 at 23:35












    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
      Mar 20 at 14:54










    • $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
      Mar 21 at 13:40










    • $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
      Mar 21 at 14:58
















    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
      Mar 20 at 14:54










    • $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
      Mar 21 at 13:40










    • $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
      Mar 21 at 14:58














    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 Mar 20 at 14:53









    RomanRoman

    3,530819




    3,530819











    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      Mar 20 at 14:54










    • $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
      Mar 21 at 13:40










    • $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
      Mar 21 at 14:58

















    • $begingroup$
      Clean solution, copying it...
      $endgroup$
      – MikeY
      Mar 20 at 14:54










    • $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
      Mar 21 at 13:40










    • $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
      Mar 21 at 14:58
















    $begingroup$
    Clean solution, copying it...
    $endgroup$
    – MikeY
    Mar 20 at 14:54




    $begingroup$
    Clean solution, copying it...
    $endgroup$
    – MikeY
    Mar 20 at 14:54












    $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
    Mar 21 at 13:40




    $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
    Mar 21 at 13:40












    $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
    Mar 21 at 14:58





    $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
    Mar 21 at 14:58












    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
      Mar 21 at 1:23















    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
      Mar 21 at 1:23













    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 Mar 21 at 19:21

























    answered Mar 20 at 15:39









    bill sbill s

    54.7k377157




    54.7k377157











    • $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
      Mar 21 at 1:23
















    • $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
      Mar 21 at 1:23















    $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
    Mar 21 at 1:23




    $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
    Mar 21 at 1:23











    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 Mar 21 at 15:39









        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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

            Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?