Using Mathematica, I have a list:
l={0,0,0,1,2,0,0,0,1,0,0,0,2,0,0,0}
I want to apply a function to the above list to obtain the following:
{0,0,0,1,2,2,2,2,1,1,1,1,2,2,2,2}
Essentially I want to replace the runs of 0 values with runs of the same length, but using the value of the positive integer just preceding each run of 0s.
I thought I could do this easily with FoldList, but I can't see my way through to a solution.
Many thanks.
Here is your test list:
tst = {0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0}
The following solution will be reasonably efficient:
In[31]:= Module[{n = 0}, Replace[tst, {0 :> n, x_ :> (n = x)}, {1}]]
Out[31]= {0, 0, 0, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2}
The way it works is the following: we use the fact that only the first matching rule is applied. The variable n
stores the last non-zero value encountered by the pattern-matcher during its run through the list. Initially it is set to zero. The first rule replaces 0
with the current value of n
. If it matches, replacement is made and the pattern-matcher goes on. If it does not match, then we have a non-zero value and the second rule applies, updating the value of n
. Since the Set
assignment returns back the value, the non-zero element is simply placed back. The solution should have a linear complexity in the length of the list, and is IMO a good example of the occasional utility of side effects mixed with rules.
EDIT
Here is a functional version:
In[56]:= Module[{n = 0}, Map[If[# != 0, n = #, n] &, tst]]
Out[56]= {0, 0, 0, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2}
One can check that the rule - based version is about 4 times faster for really large lists. However,
the advantage of this form is that it can easily be Compile
-d, providing extreme performance:
nzrunsC =
Compile[{{l, _Integer, 1}},
Module[{n = 0}, Map[If[# != 0, n = #, n] &, l]],
CompilationTarget -> "C"]
In[68]:= tstLarge = RandomInteger[{0,2},{10000000}];
In[69]:= nzrunsC[tstLarge];//Timing
Out[69]= {0.047,Null}
In[70]:= Module[{n = 0},Map[If[#!=0,n = #,n]&,tstLarge]];//Timing
Out[70]= {18.203,Null}
The difference is several hundred times here, and about a hundred times faster than the rule-based solution. OTOH, rule-based solution will work also with symbolic lists, not necessarily integer lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With