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
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?
zpd
not ending with{6., 0.}
deliberate? $\endgroup$ – J. M.'s ennui♦ Oct 19 '18 at 7:34Append
$\endgroup$ – b3m2a1 Oct 19 '18 at 7:35