How to do a list partition with offset, overhang, and cyclical padding?












5














I have a list as



{1,2,3,4,5,6,7,8,9}


I want to extract a new list as



{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,1}}


I've tried to search Partition, but it didn't give me what I want. Or I've missed out something. Can you please suggest me a way to do that?










share|improve this question




















  • 3




    Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
    – J. M. is computer-less
    yesterday










  • @N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
    – Henrik Schumacher
    yesterday










  • I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
    – N.T.C
    yesterday










  • I will try that next time thanks @HenrikSchumacher
    – N.T.C
    yesterday










  • N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
    – Henrik Schumacher
    yesterday
















5














I have a list as



{1,2,3,4,5,6,7,8,9}


I want to extract a new list as



{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,1}}


I've tried to search Partition, but it didn't give me what I want. Or I've missed out something. Can you please suggest me a way to do that?










share|improve this question




















  • 3




    Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
    – J. M. is computer-less
    yesterday










  • @N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
    – Henrik Schumacher
    yesterday










  • I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
    – N.T.C
    yesterday










  • I will try that next time thanks @HenrikSchumacher
    – N.T.C
    yesterday










  • N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
    – Henrik Schumacher
    yesterday














5












5








5


1





I have a list as



{1,2,3,4,5,6,7,8,9}


I want to extract a new list as



{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,1}}


I've tried to search Partition, but it didn't give me what I want. Or I've missed out something. Can you please suggest me a way to do that?










share|improve this question















I have a list as



{1,2,3,4,5,6,7,8,9}


I want to extract a new list as



{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,1}}


I've tried to search Partition, but it didn't give me what I want. Or I've missed out something. Can you please suggest me a way to do that?







list-manipulation partitions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









J. M. is computer-less

96.2k10300460




96.2k10300460










asked yesterday









N.T.C

42628




42628








  • 3




    Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
    – J. M. is computer-less
    yesterday










  • @N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
    – Henrik Schumacher
    yesterday










  • I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
    – N.T.C
    yesterday










  • I will try that next time thanks @HenrikSchumacher
    – N.T.C
    yesterday










  • N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
    – Henrik Schumacher
    yesterday














  • 3




    Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
    – J. M. is computer-less
    yesterday










  • @N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
    – Henrik Schumacher
    yesterday










  • I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
    – N.T.C
    yesterday










  • I will try that next time thanks @HenrikSchumacher
    – N.T.C
    yesterday










  • N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
    – Henrik Schumacher
    yesterday








3




3




Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
– J. M. is computer-less
yesterday




Partition[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2, 1, 1]; look up the fourth argument of Partition in the docs.
– J. M. is computer-less
yesterday












@N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
– Henrik Schumacher
yesterday




@N.T.C. It is not seldom that a question of the form"How do I xyz?" is answered by "Use the comman Xyz. So in principle, it should be easy to find the command in the documentation. For example, this would have been a good starting point for a search, in particular the section "Rearranging & Restructuring Lists".
– Henrik Schumacher
yesterday












I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
– N.T.C
yesterday




I did, but didn't understand it. Could you please elaborate a bit more about each parameter?
– N.T.C
yesterday












I will try that next time thanks @HenrikSchumacher
– N.T.C
yesterday




I will try that next time thanks @HenrikSchumacher
– N.T.C
yesterday












N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
– Henrik Schumacher
yesterday




N.T.C, if you want to contact another user within a comment that does not directly follow another comment by that user, you can ping them with, e.g. @J.M.iscomputer-less.
– Henrik Schumacher
yesterday










4 Answers
4






active

oldest

votes


















8














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


The command



Partition[a, 2]


just partitions the input into as many nonoverlapping pairs as possible:




{{1, 2}, {3, 4}, {5, 6}, {7, 8}}




The three-argument version creates pairs with an offset of `1:



Partition[a, 2, 1]



{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}}




That is already very close to what you want!



In order to pair also the last element with the first, use the four argument version of Partition to make it cycle



Partition[a, 2, 1, {1, 1}]



{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9,
1}}




whose shorter version is



Partition[a, 2, 1, 1]


In the usage notes of Partition (?Partition), this is somewhat hidden. The notes state (highlighting by me):




Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] specifies that
the first element of list should appear at position Subscript[k, L] in
the first sublist, and the last element of list should appear at or
after position Subscript[k, R] in the last sublist. If additional
elements are needed, Partition fills them in by treating list as
cyclic.







share|improve this answer































    6














    For the specific case you can also use



    lst = Range[9];
    Transpose[{lst, RotateLeft[lst]}]



    {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 1}}




    which is the same result Partition[lst, 2, 1, 1] gives.






    share|improve this answer





























      5














      I propose you another option that works similarly to Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] but making use of other list-manipulation functions that might be useful in other contexts.



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





      share|improve this answer





























        5














        Explanation of overhangs



        This answer is in response to the comment




        I did, but didn't understand it. Could you please elaborate a bit more
        about each parameter?




        As J.M. and Henrik have noted, this problem can be solved with the fourth argument of Partition, the so-called overhangs. I will explain in detail how overhangs work.



        l = Range[3]



        {1, 2, 3}




        Partition[l, 2, 1, {-1, -1}]



        {{3, 1}, {1, 2}, {2, 3}}




        To understand why the result above is what it is, imagine that you have a window of size two that you slide across the list. Just as if you were computing a moving average over the current element and the one before it. The overhangs specify where this window starts.



        {3, 1} is the first sublist of the result. The first overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the first sublist.



        {2, 3} is the last sublist of the result. The second overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the last sublist.



        The first overhang specifies something about the first sublist in the result, and the second overhang specifies something about the last sublist in the result.



        Let's now specify that the first sublist should not wrap around to the end, while the last sublist should. This is the opposite of what we did before. Since the first sublist should not wrap around, we want the first element of the first sublist to be the first element of the original list, i.e. 1. Since the last element of the last sublist should be the first element of the original list, we want it to be 1.



        Partition[l, 2, 1, {1, 1}]



        {{1, 2}, {2, 3}, {3, 1}}




        Using this type of reasoning, you should be able to understand there as well:



        Partition[l, 2, 1, {-1, 1}]



        {{3, 1}, {1, 2}, {2, 3}, {3, 1}}




        Partition[l, 2, 1, {1, -1}]



        {{1, 2}, {2, 3}}




        This reasoning also works for larger windows. Here's an example with a window size of 3, where we specify that the third element of the first sublist should be the first element of the original list.



        Partition[Range[5], 3, 1, {3, -1}]



        {{4, 5, 1}, {5, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




        Returning to the idea of the sliding window, we can also think of the overhang as specifying that, in this last example, the sliding window should start two steps to the left of the first element. But what is to the left of the first element? As we have seen, the list is padded cyclically by default. To the left of the first element will then be the last elements of the list.



        Other paddings can be set with the fifth argument of Partition:



        Partition[Range[5], 3, 1, {3, -1}, 0]



        {{0, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




        With this setting, the elements to the left of the first element are all zero, and the same goes for the elements to the right of the last element:



        Partition[l, 2, 1, {-1, 1}, 0]



        {{0, 1}, {1, 2}, {2, 3}, {3, 0}}







        share|improve this answer



















        • 1




          Thank you so much for this wonderful and enlightening explanation. Very helpful!!
          – Jack LaVigne
          yesterday











        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%2f188923%2fhow-to-do-a-list-partition-with-offset-overhang-and-cyclical-padding%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









        8














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


        The command



        Partition[a, 2]


        just partitions the input into as many nonoverlapping pairs as possible:




        {{1, 2}, {3, 4}, {5, 6}, {7, 8}}




        The three-argument version creates pairs with an offset of `1:



        Partition[a, 2, 1]



        {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}}




        That is already very close to what you want!



        In order to pair also the last element with the first, use the four argument version of Partition to make it cycle



        Partition[a, 2, 1, {1, 1}]



        {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9,
        1}}




        whose shorter version is



        Partition[a, 2, 1, 1]


        In the usage notes of Partition (?Partition), this is somewhat hidden. The notes state (highlighting by me):




        Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] specifies that
        the first element of list should appear at position Subscript[k, L] in
        the first sublist, and the last element of list should appear at or
        after position Subscript[k, R] in the last sublist. If additional
        elements are needed, Partition fills them in by treating list as
        cyclic.







        share|improve this answer




























          8














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


          The command



          Partition[a, 2]


          just partitions the input into as many nonoverlapping pairs as possible:




          {{1, 2}, {3, 4}, {5, 6}, {7, 8}}




          The three-argument version creates pairs with an offset of `1:



          Partition[a, 2, 1]



          {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}}




          That is already very close to what you want!



          In order to pair also the last element with the first, use the four argument version of Partition to make it cycle



          Partition[a, 2, 1, {1, 1}]



          {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9,
          1}}




          whose shorter version is



          Partition[a, 2, 1, 1]


          In the usage notes of Partition (?Partition), this is somewhat hidden. The notes state (highlighting by me):




          Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] specifies that
          the first element of list should appear at position Subscript[k, L] in
          the first sublist, and the last element of list should appear at or
          after position Subscript[k, R] in the last sublist. If additional
          elements are needed, Partition fills them in by treating list as
          cyclic.







          share|improve this answer


























            8












            8








            8






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


            The command



            Partition[a, 2]


            just partitions the input into as many nonoverlapping pairs as possible:




            {{1, 2}, {3, 4}, {5, 6}, {7, 8}}




            The three-argument version creates pairs with an offset of `1:



            Partition[a, 2, 1]



            {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}}




            That is already very close to what you want!



            In order to pair also the last element with the first, use the four argument version of Partition to make it cycle



            Partition[a, 2, 1, {1, 1}]



            {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9,
            1}}




            whose shorter version is



            Partition[a, 2, 1, 1]


            In the usage notes of Partition (?Partition), this is somewhat hidden. The notes state (highlighting by me):




            Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] specifies that
            the first element of list should appear at position Subscript[k, L] in
            the first sublist, and the last element of list should appear at or
            after position Subscript[k, R] in the last sublist. If additional
            elements are needed, Partition fills them in by treating list as
            cyclic.







            share|improve this answer














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


            The command



            Partition[a, 2]


            just partitions the input into as many nonoverlapping pairs as possible:




            {{1, 2}, {3, 4}, {5, 6}, {7, 8}}




            The three-argument version creates pairs with an offset of `1:



            Partition[a, 2, 1]



            {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}}




            That is already very close to what you want!



            In order to pair also the last element with the first, use the four argument version of Partition to make it cycle



            Partition[a, 2, 1, {1, 1}]



            {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9,
            1}}




            whose shorter version is



            Partition[a, 2, 1, 1]


            In the usage notes of Partition (?Partition), this is somewhat hidden. The notes state (highlighting by me):




            Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] specifies that
            the first element of list should appear at position Subscript[k, L] in
            the first sublist, and the last element of list should appear at or
            after position Subscript[k, R] in the last sublist. If additional
            elements are needed, Partition fills them in by treating list as
            cyclic.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            Henrik Schumacher

            49.7k469141




            49.7k469141























                6














                For the specific case you can also use



                lst = Range[9];
                Transpose[{lst, RotateLeft[lst]}]



                {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 1}}




                which is the same result Partition[lst, 2, 1, 1] gives.






                share|improve this answer


























                  6














                  For the specific case you can also use



                  lst = Range[9];
                  Transpose[{lst, RotateLeft[lst]}]



                  {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 1}}




                  which is the same result Partition[lst, 2, 1, 1] gives.






                  share|improve this answer
























                    6












                    6








                    6






                    For the specific case you can also use



                    lst = Range[9];
                    Transpose[{lst, RotateLeft[lst]}]



                    {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 1}}




                    which is the same result Partition[lst, 2, 1, 1] gives.






                    share|improve this answer












                    For the specific case you can also use



                    lst = Range[9];
                    Transpose[{lst, RotateLeft[lst]}]



                    {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 9}, {9, 1}}




                    which is the same result Partition[lst, 2, 1, 1] gives.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    kglr

                    177k9198408




                    177k9198408























                        5














                        I propose you another option that works similarly to Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] but making use of other list-manipulation functions that might be useful in other contexts.



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





                        share|improve this answer


























                          5














                          I propose you another option that works similarly to Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] but making use of other list-manipulation functions that might be useful in other contexts.



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





                          share|improve this answer
























                            5












                            5








                            5






                            I propose you another option that works similarly to Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] but making use of other list-manipulation functions that might be useful in other contexts.



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





                            share|improve this answer












                            I propose you another option that works similarly to Partition[list,n,d,{Subscript[k, L],Subscript[k, R]}] but making use of other list-manipulation functions that might be useful in other contexts.



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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            Mat

                            913




                            913























                                5














                                Explanation of overhangs



                                This answer is in response to the comment




                                I did, but didn't understand it. Could you please elaborate a bit more
                                about each parameter?




                                As J.M. and Henrik have noted, this problem can be solved with the fourth argument of Partition, the so-called overhangs. I will explain in detail how overhangs work.



                                l = Range[3]



                                {1, 2, 3}




                                Partition[l, 2, 1, {-1, -1}]



                                {{3, 1}, {1, 2}, {2, 3}}




                                To understand why the result above is what it is, imagine that you have a window of size two that you slide across the list. Just as if you were computing a moving average over the current element and the one before it. The overhangs specify where this window starts.



                                {3, 1} is the first sublist of the result. The first overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the first sublist.



                                {2, 3} is the last sublist of the result. The second overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the last sublist.



                                The first overhang specifies something about the first sublist in the result, and the second overhang specifies something about the last sublist in the result.



                                Let's now specify that the first sublist should not wrap around to the end, while the last sublist should. This is the opposite of what we did before. Since the first sublist should not wrap around, we want the first element of the first sublist to be the first element of the original list, i.e. 1. Since the last element of the last sublist should be the first element of the original list, we want it to be 1.



                                Partition[l, 2, 1, {1, 1}]



                                {{1, 2}, {2, 3}, {3, 1}}




                                Using this type of reasoning, you should be able to understand there as well:



                                Partition[l, 2, 1, {-1, 1}]



                                {{3, 1}, {1, 2}, {2, 3}, {3, 1}}




                                Partition[l, 2, 1, {1, -1}]



                                {{1, 2}, {2, 3}}




                                This reasoning also works for larger windows. Here's an example with a window size of 3, where we specify that the third element of the first sublist should be the first element of the original list.



                                Partition[Range[5], 3, 1, {3, -1}]



                                {{4, 5, 1}, {5, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                Returning to the idea of the sliding window, we can also think of the overhang as specifying that, in this last example, the sliding window should start two steps to the left of the first element. But what is to the left of the first element? As we have seen, the list is padded cyclically by default. To the left of the first element will then be the last elements of the list.



                                Other paddings can be set with the fifth argument of Partition:



                                Partition[Range[5], 3, 1, {3, -1}, 0]



                                {{0, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                With this setting, the elements to the left of the first element are all zero, and the same goes for the elements to the right of the last element:



                                Partition[l, 2, 1, {-1, 1}, 0]



                                {{0, 1}, {1, 2}, {2, 3}, {3, 0}}







                                share|improve this answer



















                                • 1




                                  Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                  – Jack LaVigne
                                  yesterday
















                                5














                                Explanation of overhangs



                                This answer is in response to the comment




                                I did, but didn't understand it. Could you please elaborate a bit more
                                about each parameter?




                                As J.M. and Henrik have noted, this problem can be solved with the fourth argument of Partition, the so-called overhangs. I will explain in detail how overhangs work.



                                l = Range[3]



                                {1, 2, 3}




                                Partition[l, 2, 1, {-1, -1}]



                                {{3, 1}, {1, 2}, {2, 3}}




                                To understand why the result above is what it is, imagine that you have a window of size two that you slide across the list. Just as if you were computing a moving average over the current element and the one before it. The overhangs specify where this window starts.



                                {3, 1} is the first sublist of the result. The first overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the first sublist.



                                {2, 3} is the last sublist of the result. The second overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the last sublist.



                                The first overhang specifies something about the first sublist in the result, and the second overhang specifies something about the last sublist in the result.



                                Let's now specify that the first sublist should not wrap around to the end, while the last sublist should. This is the opposite of what we did before. Since the first sublist should not wrap around, we want the first element of the first sublist to be the first element of the original list, i.e. 1. Since the last element of the last sublist should be the first element of the original list, we want it to be 1.



                                Partition[l, 2, 1, {1, 1}]



                                {{1, 2}, {2, 3}, {3, 1}}




                                Using this type of reasoning, you should be able to understand there as well:



                                Partition[l, 2, 1, {-1, 1}]



                                {{3, 1}, {1, 2}, {2, 3}, {3, 1}}




                                Partition[l, 2, 1, {1, -1}]



                                {{1, 2}, {2, 3}}




                                This reasoning also works for larger windows. Here's an example with a window size of 3, where we specify that the third element of the first sublist should be the first element of the original list.



                                Partition[Range[5], 3, 1, {3, -1}]



                                {{4, 5, 1}, {5, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                Returning to the idea of the sliding window, we can also think of the overhang as specifying that, in this last example, the sliding window should start two steps to the left of the first element. But what is to the left of the first element? As we have seen, the list is padded cyclically by default. To the left of the first element will then be the last elements of the list.



                                Other paddings can be set with the fifth argument of Partition:



                                Partition[Range[5], 3, 1, {3, -1}, 0]



                                {{0, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                With this setting, the elements to the left of the first element are all zero, and the same goes for the elements to the right of the last element:



                                Partition[l, 2, 1, {-1, 1}, 0]



                                {{0, 1}, {1, 2}, {2, 3}, {3, 0}}







                                share|improve this answer



















                                • 1




                                  Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                  – Jack LaVigne
                                  yesterday














                                5












                                5








                                5






                                Explanation of overhangs



                                This answer is in response to the comment




                                I did, but didn't understand it. Could you please elaborate a bit more
                                about each parameter?




                                As J.M. and Henrik have noted, this problem can be solved with the fourth argument of Partition, the so-called overhangs. I will explain in detail how overhangs work.



                                l = Range[3]



                                {1, 2, 3}




                                Partition[l, 2, 1, {-1, -1}]



                                {{3, 1}, {1, 2}, {2, 3}}




                                To understand why the result above is what it is, imagine that you have a window of size two that you slide across the list. Just as if you were computing a moving average over the current element and the one before it. The overhangs specify where this window starts.



                                {3, 1} is the first sublist of the result. The first overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the first sublist.



                                {2, 3} is the last sublist of the result. The second overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the last sublist.



                                The first overhang specifies something about the first sublist in the result, and the second overhang specifies something about the last sublist in the result.



                                Let's now specify that the first sublist should not wrap around to the end, while the last sublist should. This is the opposite of what we did before. Since the first sublist should not wrap around, we want the first element of the first sublist to be the first element of the original list, i.e. 1. Since the last element of the last sublist should be the first element of the original list, we want it to be 1.



                                Partition[l, 2, 1, {1, 1}]



                                {{1, 2}, {2, 3}, {3, 1}}




                                Using this type of reasoning, you should be able to understand there as well:



                                Partition[l, 2, 1, {-1, 1}]



                                {{3, 1}, {1, 2}, {2, 3}, {3, 1}}




                                Partition[l, 2, 1, {1, -1}]



                                {{1, 2}, {2, 3}}




                                This reasoning also works for larger windows. Here's an example with a window size of 3, where we specify that the third element of the first sublist should be the first element of the original list.



                                Partition[Range[5], 3, 1, {3, -1}]



                                {{4, 5, 1}, {5, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                Returning to the idea of the sliding window, we can also think of the overhang as specifying that, in this last example, the sliding window should start two steps to the left of the first element. But what is to the left of the first element? As we have seen, the list is padded cyclically by default. To the left of the first element will then be the last elements of the list.



                                Other paddings can be set with the fifth argument of Partition:



                                Partition[Range[5], 3, 1, {3, -1}, 0]



                                {{0, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                With this setting, the elements to the left of the first element are all zero, and the same goes for the elements to the right of the last element:



                                Partition[l, 2, 1, {-1, 1}, 0]



                                {{0, 1}, {1, 2}, {2, 3}, {3, 0}}







                                share|improve this answer














                                Explanation of overhangs



                                This answer is in response to the comment




                                I did, but didn't understand it. Could you please elaborate a bit more
                                about each parameter?




                                As J.M. and Henrik have noted, this problem can be solved with the fourth argument of Partition, the so-called overhangs. I will explain in detail how overhangs work.



                                l = Range[3]



                                {1, 2, 3}




                                Partition[l, 2, 1, {-1, -1}]



                                {{3, 1}, {1, 2}, {2, 3}}




                                To understand why the result above is what it is, imagine that you have a window of size two that you slide across the list. Just as if you were computing a moving average over the current element and the one before it. The overhangs specify where this window starts.



                                {3, 1} is the first sublist of the result. The first overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the first sublist.



                                {2, 3} is the last sublist of the result. The second overhang in {-1, -1} refers to this sublist. It says that the first element in the original list should be the last element (-1) of the last sublist.



                                The first overhang specifies something about the first sublist in the result, and the second overhang specifies something about the last sublist in the result.



                                Let's now specify that the first sublist should not wrap around to the end, while the last sublist should. This is the opposite of what we did before. Since the first sublist should not wrap around, we want the first element of the first sublist to be the first element of the original list, i.e. 1. Since the last element of the last sublist should be the first element of the original list, we want it to be 1.



                                Partition[l, 2, 1, {1, 1}]



                                {{1, 2}, {2, 3}, {3, 1}}




                                Using this type of reasoning, you should be able to understand there as well:



                                Partition[l, 2, 1, {-1, 1}]



                                {{3, 1}, {1, 2}, {2, 3}, {3, 1}}




                                Partition[l, 2, 1, {1, -1}]



                                {{1, 2}, {2, 3}}




                                This reasoning also works for larger windows. Here's an example with a window size of 3, where we specify that the third element of the first sublist should be the first element of the original list.



                                Partition[Range[5], 3, 1, {3, -1}]



                                {{4, 5, 1}, {5, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                Returning to the idea of the sliding window, we can also think of the overhang as specifying that, in this last example, the sliding window should start two steps to the left of the first element. But what is to the left of the first element? As we have seen, the list is padded cyclically by default. To the left of the first element will then be the last elements of the list.



                                Other paddings can be set with the fifth argument of Partition:



                                Partition[Range[5], 3, 1, {3, -1}, 0]



                                {{0, 0, 1}, {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}}




                                With this setting, the elements to the left of the first element are all zero, and the same goes for the elements to the right of the last element:



                                Partition[l, 2, 1, {-1, 1}, 0]



                                {{0, 1}, {1, 2}, {2, 3}, {3, 0}}








                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 18 hours ago









                                Henrik Schumacher

                                49.7k469141




                                49.7k469141










                                answered yesterday









                                C. E.

                                50k397202




                                50k397202








                                • 1




                                  Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                  – Jack LaVigne
                                  yesterday














                                • 1




                                  Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                  – Jack LaVigne
                                  yesterday








                                1




                                1




                                Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                – Jack LaVigne
                                yesterday




                                Thank you so much for this wonderful and enlightening explanation. Very helpful!!
                                – Jack LaVigne
                                yesterday


















                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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.


                                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%2f188923%2fhow-to-do-a-list-partition-with-offset-overhang-and-cyclical-padding%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

                                1300-talet

                                1300-talet

                                Display a custom attribute below product name in the front-end Magento 1.9.3.8