Fastest/cleanest way to pad data with zero-data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I want to pad a set of data in x, y pairs with x, 0, x, 0 on each side of each data point.



I know I can do this like so:



zeroPaddedData[xdata_, ydata_] :=

With[padding =
Transpose[xdata, ConstantArray[0., Length@xdata]],
Append[
Riffle[
Riffle[
padding,
Transpose[xdata, ydata]
],
padding,
3
],
xdata[[-1]], 0.
]
]


which, for example, gives:



zpd = zeroPaddedData @@ Transpose@Table[x, Sin[x], x, 0, 2 Pi, .5]

0., 0., 0., 0., 0., 0., 0.5, 0., 0.5, 0.479426, 0.5,
0., 1., 0., 1., 0.841471, 1., 0., 1.5, 0., 1.5,
0.997495, 1.5, 0., 2., 0., 2., 0.909297, 2., 0., 2.5,
0., 2.5, 0.598472, 2.5, 0., 3., 0., 3., 0.14112, 3.,
0., 3.5, 0., 3.5, -0.350783, 3.5, 0., 4.,
0., 4., -0.756802, 4., 0., 4.5, 0., 4.5, -0.97753, 4.5,
0., 5., 0., 5., -0.958924, 5., 0., 5.5,
0., 5.5, -0.70554, 5.5, 0., 6., 0., 6., -0.279415, 6., 0.


Which will plot out like so:



zpd // ListLinePlot


enter image description here



But this is slow over larger data sets:



dats = Transpose@Table[x, Sin[x], x, 0, 2 Pi, .001];

zeroPaddedData @@ dats // RepeatedTiming // First

0.0039


How can I do this faster/cleaner?










share|improve this question























  • Is zpd not ending with 6., 0. deliberate?
    – J. M. is computer-less♦
    2 hours ago










  • @J.M.iscomputer-less nope I just accidentally a result from before I added the Append
    – b3m2a1
    2 hours ago














up vote
3
down vote

favorite












I want to pad a set of data in x, y pairs with x, 0, x, 0 on each side of each data point.



I know I can do this like so:



zeroPaddedData[xdata_, ydata_] :=

With[padding =
Transpose[xdata, ConstantArray[0., Length@xdata]],
Append[
Riffle[
Riffle[
padding,
Transpose[xdata, ydata]
],
padding,
3
],
xdata[[-1]], 0.
]
]


which, for example, gives:



zpd = zeroPaddedData @@ Transpose@Table[x, Sin[x], x, 0, 2 Pi, .5]

0., 0., 0., 0., 0., 0., 0.5, 0., 0.5, 0.479426, 0.5,
0., 1., 0., 1., 0.841471, 1., 0., 1.5, 0., 1.5,
0.997495, 1.5, 0., 2., 0., 2., 0.909297, 2., 0., 2.5,
0., 2.5, 0.598472, 2.5, 0., 3., 0., 3., 0.14112, 3.,
0., 3.5, 0., 3.5, -0.350783, 3.5, 0., 4.,
0., 4., -0.756802, 4., 0., 4.5, 0., 4.5, -0.97753, 4.5,
0., 5., 0., 5., -0.958924, 5., 0., 5.5,
0., 5.5, -0.70554, 5.5, 0., 6., 0., 6., -0.279415, 6., 0.


Which will plot out like so:



zpd // ListLinePlot


enter image description here



But this is slow over larger data sets:



dats = Transpose@Table[x, Sin[x], x, 0, 2 Pi, .001];

zeroPaddedData @@ dats // RepeatedTiming // First

0.0039


How can I do this faster/cleaner?










share|improve this question























  • Is zpd not ending with 6., 0. deliberate?
    – J. M. is computer-less♦
    2 hours ago










  • @J.M.iscomputer-less nope I just accidentally a result from before I added the Append
    – b3m2a1
    2 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I want to pad a set of data in x, y pairs with x, 0, x, 0 on each side of each data point.



I know I can do this like so:



zeroPaddedData[xdata_, ydata_] :=

With[padding =
Transpose[xdata, ConstantArray[0., Length@xdata]],
Append[
Riffle[
Riffle[
padding,
Transpose[xdata, ydata]
],
padding,
3
],
xdata[[-1]], 0.
]
]


which, for example, gives:



zpd = zeroPaddedData @@ Transpose@Table[x, Sin[x], x, 0, 2 Pi, .5]

0., 0., 0., 0., 0., 0., 0.5, 0., 0.5, 0.479426, 0.5,
0., 1., 0., 1., 0.841471, 1., 0., 1.5, 0., 1.5,
0.997495, 1.5, 0., 2., 0., 2., 0.909297, 2., 0., 2.5,
0., 2.5, 0.598472, 2.5, 0., 3., 0., 3., 0.14112, 3.,
0., 3.5, 0., 3.5, -0.350783, 3.5, 0., 4.,
0., 4., -0.756802, 4., 0., 4.5, 0., 4.5, -0.97753, 4.5,
0., 5., 0., 5., -0.958924, 5., 0., 5.5,
0., 5.5, -0.70554, 5.5, 0., 6., 0., 6., -0.279415, 6., 0.


Which will plot out like so:



zpd // ListLinePlot


enter image description here



But this is slow over larger data sets:



dats = Transpose@Table[x, Sin[x], x, 0, 2 Pi, .001];

zeroPaddedData @@ dats // RepeatedTiming // First

0.0039


How can I do this faster/cleaner?










share|improve this question















I want to pad a set of data in x, y pairs with x, 0, x, 0 on each side of each data point.



I know I can do this like so:



zeroPaddedData[xdata_, ydata_] :=

With[padding =
Transpose[xdata, ConstantArray[0., Length@xdata]],
Append[
Riffle[
Riffle[
padding,
Transpose[xdata, ydata]
],
padding,
3
],
xdata[[-1]], 0.
]
]


which, for example, gives:



zpd = zeroPaddedData @@ Transpose@Table[x, Sin[x], x, 0, 2 Pi, .5]

0., 0., 0., 0., 0., 0., 0.5, 0., 0.5, 0.479426, 0.5,
0., 1., 0., 1., 0.841471, 1., 0., 1.5, 0., 1.5,
0.997495, 1.5, 0., 2., 0., 2., 0.909297, 2., 0., 2.5,
0., 2.5, 0.598472, 2.5, 0., 3., 0., 3., 0.14112, 3.,
0., 3.5, 0., 3.5, -0.350783, 3.5, 0., 4.,
0., 4., -0.756802, 4., 0., 4.5, 0., 4.5, -0.97753, 4.5,
0., 5., 0., 5., -0.958924, 5., 0., 5.5,
0., 5.5, -0.70554, 5.5, 0., 6., 0., 6., -0.279415, 6., 0.


Which will plot out like so:



zpd // ListLinePlot


enter image description here



But this is slow over larger data sets:



dats = Transpose@Table[x, Sin[x], x, 0, 2 Pi, .001];

zeroPaddedData @@ dats // RepeatedTiming // First

0.0039


How can I do this faster/cleaner?







list-manipulation performance-tuning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 2 hours ago









b3m2a1

25k254147




25k254147











  • Is zpd not ending with 6., 0. deliberate?
    – J. M. is computer-less♦
    2 hours ago










  • @J.M.iscomputer-less nope I just accidentally a result from before I added the Append
    – b3m2a1
    2 hours ago
















  • Is zpd not ending with 6., 0. deliberate?
    – J. M. is computer-less♦
    2 hours ago










  • @J.M.iscomputer-less nope I just accidentally a result from before I added the Append
    – b3m2a1
    2 hours ago















Is zpd not ending with 6., 0. deliberate?
– J. M. is computer-less♦
2 hours ago




Is zpd not ending with 6., 0. deliberate?
– J. M. is computer-less♦
2 hours ago












@J.M.iscomputer-less nope I just accidentally a result from before I added the Append
– b3m2a1
2 hours ago




@J.M.iscomputer-less nope I just accidentally a result from before I added the Append
– b3m2a1
2 hours ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













One reason for slowness is that the input data was not packed. Moreover Transpose is often faster than Riffle:



xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];

f[xdata_, ydata_] := Transpose[
Flatten[Transpose[ConstantArray[xdata, 3]]],
With[o = ConstantArray[0., Length[ydata]],
Flatten[Transpose[o, ydata, o]]
]
]

a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First
b = f[xdata, ydata]; // RepeatedTiming // First
a == b



0.030



0.00165



True







share|improve this answer



























    up vote
    1
    down vote













    Slower but simple



    tab = Table[x, Sin[x], x, 0, 2 Pi, .5];
    SequenceReplace[tab, a_,b_:>Sequence[a,0,a,b,a,0]] == zpd



    True




    Update: Using Upsample on the second argument gives a slight improvement over Henrik's f:



    ClearAll[zPad]
    zPad[xd_, yd_] := Transpose[Flatten[ConstantArray[xd, 3], 2, 1], Upsample[yd, 3, 2]];

    xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];
    a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First



    0.024




    b = f[xdata, ydata]; // RepeatedTiming // First



    0.0017




    c = zPad[xdata, ydata]; // RepeatedTiming // First 



    0.0016




    a == b == c



    True




    Interesting to note that although Upsample is almost twice as fast as With[o = ConstantArray[0., Length[ydata]], Flatten[Transpose[o, ydata, o] ]] this advantage is not retained when combined with other steps:



    r1 = Upsample[ydata, 3, 2] ; // RepeatedTiming // First



    0.00061




    r2 = With[o = ConstantArray[0., Length[ydata]], 
    Flatten[Transpose[o, ydata, o] ]]; // RepeatedTiming // First



    0.0013




    r1 == r2



    True







    share|improve this answer






















      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f184205%2ffastest-cleanest-way-to-pad-data-with-zero-data%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      One reason for slowness is that the input data was not packed. Moreover Transpose is often faster than Riffle:



      xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];

      f[xdata_, ydata_] := Transpose[
      Flatten[Transpose[ConstantArray[xdata, 3]]],
      With[o = ConstantArray[0., Length[ydata]],
      Flatten[Transpose[o, ydata, o]]
      ]
      ]

      a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First
      b = f[xdata, ydata]; // RepeatedTiming // First
      a == b



      0.030



      0.00165



      True







      share|improve this answer
























        up vote
        2
        down vote













        One reason for slowness is that the input data was not packed. Moreover Transpose is often faster than Riffle:



        xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];

        f[xdata_, ydata_] := Transpose[
        Flatten[Transpose[ConstantArray[xdata, 3]]],
        With[o = ConstantArray[0., Length[ydata]],
        Flatten[Transpose[o, ydata, o]]
        ]
        ]

        a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First
        b = f[xdata, ydata]; // RepeatedTiming // First
        a == b



        0.030



        0.00165



        True







        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          One reason for slowness is that the input data was not packed. Moreover Transpose is often faster than Riffle:



          xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];

          f[xdata_, ydata_] := Transpose[
          Flatten[Transpose[ConstantArray[xdata, 3]]],
          With[o = ConstantArray[0., Length[ydata]],
          Flatten[Transpose[o, ydata, o]]
          ]
          ]

          a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First
          b = f[xdata, ydata]; // RepeatedTiming // First
          a == b



          0.030



          0.00165



          True







          share|improve this answer












          One reason for slowness is that the input data was not packed. Moreover Transpose is often faster than Riffle:



          xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];

          f[xdata_, ydata_] := Transpose[
          Flatten[Transpose[ConstantArray[xdata, 3]]],
          With[o = ConstantArray[0., Length[ydata]],
          Flatten[Transpose[o, ydata, o]]
          ]
          ]

          a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First
          b = f[xdata, ydata]; // RepeatedTiming // First
          a == b



          0.030



          0.00165



          True








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Henrik Schumacher

          42.3k261125




          42.3k261125




















              up vote
              1
              down vote













              Slower but simple



              tab = Table[x, Sin[x], x, 0, 2 Pi, .5];
              SequenceReplace[tab, a_,b_:>Sequence[a,0,a,b,a,0]] == zpd



              True




              Update: Using Upsample on the second argument gives a slight improvement over Henrik's f:



              ClearAll[zPad]
              zPad[xd_, yd_] := Transpose[Flatten[ConstantArray[xd, 3], 2, 1], Upsample[yd, 3, 2]];

              xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];
              a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First



              0.024




              b = f[xdata, ydata]; // RepeatedTiming // First



              0.0017




              c = zPad[xdata, ydata]; // RepeatedTiming // First 



              0.0016




              a == b == c



              True




              Interesting to note that although Upsample is almost twice as fast as With[o = ConstantArray[0., Length[ydata]], Flatten[Transpose[o, ydata, o] ]] this advantage is not retained when combined with other steps:



              r1 = Upsample[ydata, 3, 2] ; // RepeatedTiming // First



              0.00061




              r2 = With[o = ConstantArray[0., Length[ydata]], 
              Flatten[Transpose[o, ydata, o] ]]; // RepeatedTiming // First



              0.0013




              r1 == r2



              True







              share|improve this answer


























                up vote
                1
                down vote













                Slower but simple



                tab = Table[x, Sin[x], x, 0, 2 Pi, .5];
                SequenceReplace[tab, a_,b_:>Sequence[a,0,a,b,a,0]] == zpd



                True




                Update: Using Upsample on the second argument gives a slight improvement over Henrik's f:



                ClearAll[zPad]
                zPad[xd_, yd_] := Transpose[Flatten[ConstantArray[xd, 3], 2, 1], Upsample[yd, 3, 2]];

                xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];
                a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First



                0.024




                b = f[xdata, ydata]; // RepeatedTiming // First



                0.0017




                c = zPad[xdata, ydata]; // RepeatedTiming // First 



                0.0016




                a == b == c



                True




                Interesting to note that although Upsample is almost twice as fast as With[o = ConstantArray[0., Length[ydata]], Flatten[Transpose[o, ydata, o] ]] this advantage is not retained when combined with other steps:



                r1 = Upsample[ydata, 3, 2] ; // RepeatedTiming // First



                0.00061




                r2 = With[o = ConstantArray[0., Length[ydata]], 
                Flatten[Transpose[o, ydata, o] ]]; // RepeatedTiming // First



                0.0013




                r1 == r2



                True







                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Slower but simple



                  tab = Table[x, Sin[x], x, 0, 2 Pi, .5];
                  SequenceReplace[tab, a_,b_:>Sequence[a,0,a,b,a,0]] == zpd



                  True




                  Update: Using Upsample on the second argument gives a slight improvement over Henrik's f:



                  ClearAll[zPad]
                  zPad[xd_, yd_] := Transpose[Flatten[ConstantArray[xd, 3], 2, 1], Upsample[yd, 3, 2]];

                  xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];
                  a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First



                  0.024




                  b = f[xdata, ydata]; // RepeatedTiming // First



                  0.0017




                  c = zPad[xdata, ydata]; // RepeatedTiming // First 



                  0.0016




                  a == b == c



                  True




                  Interesting to note that although Upsample is almost twice as fast as With[o = ConstantArray[0., Length[ydata]], Flatten[Transpose[o, ydata, o] ]] this advantage is not retained when combined with other steps:



                  r1 = Upsample[ydata, 3, 2] ; // RepeatedTiming // First



                  0.00061




                  r2 = With[o = ConstantArray[0., Length[ydata]], 
                  Flatten[Transpose[o, ydata, o] ]]; // RepeatedTiming // First



                  0.0013




                  r1 == r2



                  True







                  share|improve this answer














                  Slower but simple



                  tab = Table[x, Sin[x], x, 0, 2 Pi, .5];
                  SequenceReplace[tab, a_,b_:>Sequence[a,0,a,b,a,0]] == zpd



                  True




                  Update: Using Upsample on the second argument gives a slight improvement over Henrik's f:



                  ClearAll[zPad]
                  zPad[xd_, yd_] := Transpose[Flatten[ConstantArray[xd, 3], 2, 1], Upsample[yd, 3, 2]];

                  xdata, ydata = Transpose[Map[x [Function] x, Sin[x], Range[0., 2 Pi, .0001]]];
                  a = zeroPaddedData[xdata, ydata]; // RepeatedTiming // First



                  0.024




                  b = f[xdata, ydata]; // RepeatedTiming // First



                  0.0017




                  c = zPad[xdata, ydata]; // RepeatedTiming // First 



                  0.0016




                  a == b == c



                  True




                  Interesting to note that although Upsample is almost twice as fast as With[o = ConstantArray[0., Length[ydata]], Flatten[Transpose[o, ydata, o] ]] this advantage is not retained when combined with other steps:



                  r1 = Upsample[ydata, 3, 2] ; // RepeatedTiming // First



                  0.00061




                  r2 = With[o = ConstantArray[0., Length[ydata]], 
                  Flatten[Transpose[o, ydata, o] ]]; // RepeatedTiming // First



                  0.0013




                  r1 == r2



                  True








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 39 mins ago

























                  answered 1 hour ago









                  kglr

                  166k8188389




                  166k8188389



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184205%2ffastest-cleanest-way-to-pad-data-with-zero-data%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery