Fastest/cleanest way to pad data with zero-data
Clash 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
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
add a comment |Â
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
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
Iszpd
not ending with6., 0.
deliberate?
– J. M. is computer-less♦
2 hours ago
@J.M.iscomputer-less nope I just accidentally a result from before I added theAppend
– b3m2a1
2 hours ago
add a comment |Â
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
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
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?
list-manipulation performance-tuning
list-manipulation performance-tuning
edited 2 hours ago
asked 2 hours ago
b3m2a1
25k254147
25k254147
Iszpd
not ending with6., 0.
deliberate?
– J. M. is computer-less♦
2 hours ago
@J.M.iscomputer-less nope I just accidentally a result from before I added theAppend
– b3m2a1
2 hours ago
add a comment |Â
Iszpd
not ending with6., 0.
deliberate?
– J. M. is computer-less♦
2 hours ago
@J.M.iscomputer-less nope I just accidentally a result from before I added theAppend
– 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 2 hours ago


Henrik Schumacher
42.3k261125
42.3k261125
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited 39 mins ago
answered 1 hour ago
kglr
166k8188389
166k8188389
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Is
zpd
not ending with6., 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