Is it possible to re-order a List of continuous entries?












3












$begingroup$


I am given a list



a = {1.1,2.1,3.1,4.1,...}


As you can see this is not a list of the form



{{1.1},{2.1},{3.1},{4.1},...}


Now I was wondering whether it is possible to write a swap function



swap[a,{1,3}]


which would swap the first and third entry such that I get



{3.1,2.1,1.1,4.1,...}


As a result.



Any help will be greatly appreciated !



If it is not possible please just tell me then I'll start looking for other ways to approach this problem.










share|improve this question











$endgroup$












  • $begingroup$
    I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:09












  • $begingroup$
    You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
    $endgroup$
    – CatoMaths
    Feb 19 at 11:16










  • $begingroup$
    I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:20










  • $begingroup$
    Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
    $endgroup$
    – CatoMaths
    Feb 19 at 11:33












  • $begingroup$
    Dupe of 191781?
    $endgroup$
    – Rabbit
    Feb 19 at 17:14
















3












$begingroup$


I am given a list



a = {1.1,2.1,3.1,4.1,...}


As you can see this is not a list of the form



{{1.1},{2.1},{3.1},{4.1},...}


Now I was wondering whether it is possible to write a swap function



swap[a,{1,3}]


which would swap the first and third entry such that I get



{3.1,2.1,1.1,4.1,...}


As a result.



Any help will be greatly appreciated !



If it is not possible please just tell me then I'll start looking for other ways to approach this problem.










share|improve this question











$endgroup$












  • $begingroup$
    I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:09












  • $begingroup$
    You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
    $endgroup$
    – CatoMaths
    Feb 19 at 11:16










  • $begingroup$
    I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:20










  • $begingroup$
    Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
    $endgroup$
    – CatoMaths
    Feb 19 at 11:33












  • $begingroup$
    Dupe of 191781?
    $endgroup$
    – Rabbit
    Feb 19 at 17:14














3












3








3





$begingroup$


I am given a list



a = {1.1,2.1,3.1,4.1,...}


As you can see this is not a list of the form



{{1.1},{2.1},{3.1},{4.1},...}


Now I was wondering whether it is possible to write a swap function



swap[a,{1,3}]


which would swap the first and third entry such that I get



{3.1,2.1,1.1,4.1,...}


As a result.



Any help will be greatly appreciated !



If it is not possible please just tell me then I'll start looking for other ways to approach this problem.










share|improve this question











$endgroup$




I am given a list



a = {1.1,2.1,3.1,4.1,...}


As you can see this is not a list of the form



{{1.1},{2.1},{3.1},{4.1},...}


Now I was wondering whether it is possible to write a swap function



swap[a,{1,3}]


which would swap the first and third entry such that I get



{3.1,2.1,1.1,4.1,...}


As a result.



Any help will be greatly appreciated !



If it is not possible please just tell me then I'll start looking for other ways to approach this problem.







list-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 19 at 16:49









Glorfindel

2631311




2631311










asked Feb 19 at 11:02









CatoMathsCatoMaths

274




274












  • $begingroup$
    I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:09












  • $begingroup$
    You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
    $endgroup$
    – CatoMaths
    Feb 19 at 11:16










  • $begingroup$
    I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:20










  • $begingroup$
    Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
    $endgroup$
    – CatoMaths
    Feb 19 at 11:33












  • $begingroup$
    Dupe of 191781?
    $endgroup$
    – Rabbit
    Feb 19 at 17:14


















  • $begingroup$
    I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:09












  • $begingroup$
    You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
    $endgroup$
    – CatoMaths
    Feb 19 at 11:16










  • $begingroup$
    I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
    $endgroup$
    – Henrik Schumacher
    Feb 19 at 11:20










  • $begingroup$
    Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
    $endgroup$
    – CatoMaths
    Feb 19 at 11:33












  • $begingroup$
    Dupe of 191781?
    $endgroup$
    – Rabbit
    Feb 19 at 17:14
















$begingroup$
I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
$endgroup$
– Henrik Schumacher
Feb 19 at 11:09






$begingroup$
I do not understand is this statement: "So I cannot target single entries using a command like [...]". Moroever, please adopt the habit of positing complete minimal examples. Mentioning the symbol MinVarGeneralThermal1 is totally irrelevant for the problem at hand, isn't it? You question is merely about reording a given list.
$endgroup$
– Henrik Schumacher
Feb 19 at 11:09














$begingroup$
You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
$endgroup$
– CatoMaths
Feb 19 at 11:16




$begingroup$
You are right, I corrected the name of the function. What I meant with 'function1[T, E, d][[2]]' is that this allows to target a single entry of a list, which would correspond to {2.1} in my example
$endgroup$
– CatoMaths
Feb 19 at 11:16












$begingroup$
I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
$endgroup$
– Henrik Schumacher
Feb 19 at 11:20




$begingroup$
I took the freedom to modify your post in order to remove the function. Please check whether this is in accordance with your actual question. Also, please rephrase the part "So I cannot target single entries using a command like [...]" because I still don't get what you are asking for.
$endgroup$
– Henrik Schumacher
Feb 19 at 11:20












$begingroup$
Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
$endgroup$
– CatoMaths
Feb 19 at 11:33






$begingroup$
Oh well, this statement was written to help the readers understand my issue. But it is not necessary to understand the problem by any means. I will delete it. And thank you for the edit !
$endgroup$
– CatoMaths
Feb 19 at 11:33














$begingroup$
Dupe of 191781?
$endgroup$
– Rabbit
Feb 19 at 17:14




$begingroup$
Dupe of 191781?
$endgroup$
– Rabbit
Feb 19 at 17:14










1 Answer
1






active

oldest

votes


















4












$begingroup$

a = {1.1, 2.1, 3.1, 4.1};

swap[a_, {i_, j_}] := Module[{b = a},
b[[{j, i}]] = b[[{i, j}]];
b
]

swap[a, {3, 1}]



{3.1, 2.1, 1.1, 4.1}




Alternatively, you may also use ReplacePart as follows:



swap[a_, {i_, j_}] := ReplacePart[a, {i -> a[[j]], j -> a[[i]]}]





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%2f191804%2fis-it-possible-to-re-order-a-list-of-continuous-entries%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4












    $begingroup$

    a = {1.1, 2.1, 3.1, 4.1};

    swap[a_, {i_, j_}] := Module[{b = a},
    b[[{j, i}]] = b[[{i, j}]];
    b
    ]

    swap[a, {3, 1}]



    {3.1, 2.1, 1.1, 4.1}




    Alternatively, you may also use ReplacePart as follows:



    swap[a_, {i_, j_}] := ReplacePart[a, {i -> a[[j]], j -> a[[i]]}]





    share|improve this answer









    $endgroup$


















      4












      $begingroup$

      a = {1.1, 2.1, 3.1, 4.1};

      swap[a_, {i_, j_}] := Module[{b = a},
      b[[{j, i}]] = b[[{i, j}]];
      b
      ]

      swap[a, {3, 1}]



      {3.1, 2.1, 1.1, 4.1}




      Alternatively, you may also use ReplacePart as follows:



      swap[a_, {i_, j_}] := ReplacePart[a, {i -> a[[j]], j -> a[[i]]}]





      share|improve this answer









      $endgroup$
















        4












        4








        4





        $begingroup$

        a = {1.1, 2.1, 3.1, 4.1};

        swap[a_, {i_, j_}] := Module[{b = a},
        b[[{j, i}]] = b[[{i, j}]];
        b
        ]

        swap[a, {3, 1}]



        {3.1, 2.1, 1.1, 4.1}




        Alternatively, you may also use ReplacePart as follows:



        swap[a_, {i_, j_}] := ReplacePart[a, {i -> a[[j]], j -> a[[i]]}]





        share|improve this answer









        $endgroup$



        a = {1.1, 2.1, 3.1, 4.1};

        swap[a_, {i_, j_}] := Module[{b = a},
        b[[{j, i}]] = b[[{i, j}]];
        b
        ]

        swap[a, {3, 1}]



        {3.1, 2.1, 1.1, 4.1}




        Alternatively, you may also use ReplacePart as follows:



        swap[a_, {i_, j_}] := ReplacePart[a, {i -> a[[j]], j -> a[[i]]}]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 19 at 11:55









        Henrik SchumacherHenrik Schumacher

        55.1k475154




        55.1k475154






























            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%2f191804%2fis-it-possible-to-re-order-a-list-of-continuous-entries%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 to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?