Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:07:04+00:00 2026-05-24T08:07:04+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:07:05+00:00Added an answer on May 24, 2026 at 8:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mathematica list in which I want to replace all 2s with
I'm using Mathematica 7. I have an interpolated function, here's an example: pressures =
Hi I am using Mathematica 5.2. Suppose I have an array list like In[2]:=lst=Tuples[{0,1},4]
I have a list of dates and values in the format: {{{dateInfo1},value1},{{dateInfo2},value2},...,{{dateInfoN},valueN}} With some
I'm using python, and I want a function that takes a string containing a
I'm using Mathematica and have a set of variables (A,B,C,D,...) with properties A=(blue, big,
I have the following list of pairs of data: pairs = {{3, John}, {1,
Anyone who's tried to study mathematics using online resources will have come across these
I need an array of 820 zeros for using with a mathematical function. In
Using online interfaces to a version control system is a nice way to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.