List, map function based on a conditionConditional TableHow to replace an element in a list based on the...

A reccomended structured approach to self studying music theory for songwriting

How to render "have ideas above his station" into German

Subgroup generated by a subgroup and a conjugate of it

Reducing contention in thread-safe LruCache

C++ Least cost swapping 2

Polar contour plot in Mathematica?

What is the opposite of "hunger level"?

Pocket Clarketech

Best model for precedence constraints within scheduling problem

Why don't modern jet engines use forced exhaust mixing?

The Roommates' Dilemma

How do I answer an interview question about how to handle a hard deadline I won't be able to meet?

Quick destruction of a helium filled airship?

Spongy green glass found on graves

What would cause a nuclear power plant to break down after 2000 years, but not sooner?

What exactly happened to the 18 crew members who were reported as "missing" in "Q Who"?

Are unaudited server logs admissible in a court of law?

Why was ramjet fuel used as hydraulic fluid during Saturn V checkout?

Why can't I see 1861 / 1871 census entries on Freecen website when I can see them on Ancestry website?

μονάδαι as plural form of μονάς

What are some tips and tricks for finding the cheapest flight when luggage and other fees are not revealed until far into the booking process?

Photoshop older default brushes

Did Michelle Obama have a staff of 23; and Melania have a staff of 4?

Why do aircraft leave cruising altitude long before landing just to circle?



List, map function based on a condition


Conditional TableHow to replace an element in a list based on the value of the next element?Indexed Map ThreadChange some elements at the second level of a list based on some conditionsHow-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pairInserting a condition inside a FOR loopFilter a nested list based on conditions on its elements






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







5












$begingroup$


I have the following list:



input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 
0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1}}


there are two conditions:




  • When a '0' is followed by '1', the value of '0' change to .5

  • When a '1' is followed by '0', the value of '0' change to .5


the desired output is:



output = [{{0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5, 
0, .5, 1, .5, 1}, {.5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1}, {1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1}}]


Who has a suggestion how to get the desired output










share|improve this question









$endgroup$










  • 2




    $begingroup$
    should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
    $endgroup$
    – user1066
    2 days ago


















5












$begingroup$


I have the following list:



input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 
0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1}}


there are two conditions:




  • When a '0' is followed by '1', the value of '0' change to .5

  • When a '1' is followed by '0', the value of '0' change to .5


the desired output is:



output = [{{0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5, 
0, .5, 1, .5, 1}, {.5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1}, {1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1}}]


Who has a suggestion how to get the desired output










share|improve this question









$endgroup$










  • 2




    $begingroup$
    should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
    $endgroup$
    – user1066
    2 days ago














5












5








5





$begingroup$


I have the following list:



input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 
0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1}}


there are two conditions:




  • When a '0' is followed by '1', the value of '0' change to .5

  • When a '1' is followed by '0', the value of '0' change to .5


the desired output is:



output = [{{0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5, 
0, .5, 1, .5, 1}, {.5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1}, {1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1}}]


Who has a suggestion how to get the desired output










share|improve this question









$endgroup$




I have the following list:



input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 
0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1}}


there are two conditions:




  • When a '0' is followed by '1', the value of '0' change to .5

  • When a '1' is followed by '0', the value of '0' change to .5


the desired output is:



output = [{{0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5, 
0, .5, 1, .5, 1}, {.5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1}, {1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1}}]


Who has a suggestion how to get the desired output







list-manipulation conditional map






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Michiel van MensMichiel van Mens

9576 silver badges19 bronze badges




9576 silver badges19 bronze badges











  • 2




    $begingroup$
    should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
    $endgroup$
    – user1066
    2 days ago














  • 2




    $begingroup$
    should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
    $endgroup$
    – user1066
    2 days ago








2




2




$begingroup$
should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
$endgroup$
– user1066
2 days ago




$begingroup$
should desired[[3]] be {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}?
$endgroup$
– user1066
2 days ago










4 Answers
4






active

oldest

votes


















8












$begingroup$

Using ReplaceRepeated (//.) and pattern matching:



input //. {{x___, 0, 1, y___} :> {x, 0.5, 1, y}, {x___, 1, 0, y___} :> {x, 1, 0.5, y}}



{{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.






share|improve this answer









$endgroup$















  • $begingroup$
    Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
    $endgroup$
    – Ulrich Neumann
    2 days ago






  • 1




    $begingroup$
    /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
    $endgroup$
    – eyorble
    2 days ago










  • $begingroup$
    Thanks, I have to think about it.
    $endgroup$
    – Ulrich Neumann
    2 days ago



















8












$begingroup$

You can also use a combination of SequenceReplace and FixedPoint:



f = Map[SequenceReplace[{{0, 1} -> Sequence[.5, 1], {1, 0} -> Sequence[1, .5]}]], 

FixedPoint[f, input]



{{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1},

{0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},

{1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




You can also use Nest in place of FixedPoint:



Nest[f , input, 2] == %



True







share|improve this answer











$endgroup$























    4












    $begingroup$

    For a rewriting problem, use rewriting explicitly:



    input //. {
    {x___, 0, 1, y___} -> {x, 0.5, 1, y},
    {x___, 1, 0, y___} -> {x, 1, 0.5, y}
    }
    (*
    {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
    {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
    {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}
    *)





    share|improve this answer









    $endgroup$























      0












      $begingroup$

      This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show



      foo[input_List] := Module[{n, i, current, next, before},
      n = Length[input];

      (*handle edge cases*)
      If[n == 1, Return[input, Module]];
      If[n == 2,
      Return[If[input[[1]] == 0 && input[[2]] == 1, {0.5, 1},
      If[input[[2]] == 0 && input[[1]] == 1, {1, 0.5}, input]],
      Module]];

      (*general case for list of length [GreaterEqual] 3*)
      Table[
      current = input[[i]];
      If[i == 1,
      If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
      ,
      If[i == n,
      If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
      ,
      before = input[[i - 1]];
      next = input[[i + 1]];
      If[current == 0 && next == 1, 0.5,
      If[current == 0 && before == 1, 0.5, current]]
      ]
      ],
      {i, 1, Length[input]}
      ]
      ]


      input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
      0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
      1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
      0, 1, 0, 1}};


      Now map foo on the input



       result = foo[#] & /@ input


      gives



      {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 
      0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
      1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
      0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}


      I suspect there might be a shorter way to do this if one works harder on it.






      share|improve this answer











      $endgroup$


















        Your Answer








        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        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%2f203760%2flist-map-function-based-on-a-condition%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8












        $begingroup$

        Using ReplaceRepeated (//.) and pattern matching:



        input //. {{x___, 0, 1, y___} :> {x, 0.5, 1, y}, {x___, 1, 0, y___} :> {x, 1, 0.5, y}}



        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




        Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.






        share|improve this answer









        $endgroup$















        • $begingroup$
          Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
          $endgroup$
          – Ulrich Neumann
          2 days ago






        • 1




          $begingroup$
          /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
          $endgroup$
          – eyorble
          2 days ago










        • $begingroup$
          Thanks, I have to think about it.
          $endgroup$
          – Ulrich Neumann
          2 days ago
















        8












        $begingroup$

        Using ReplaceRepeated (//.) and pattern matching:



        input //. {{x___, 0, 1, y___} :> {x, 0.5, 1, y}, {x___, 1, 0, y___} :> {x, 1, 0.5, y}}



        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




        Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.






        share|improve this answer









        $endgroup$















        • $begingroup$
          Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
          $endgroup$
          – Ulrich Neumann
          2 days ago






        • 1




          $begingroup$
          /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
          $endgroup$
          – eyorble
          2 days ago










        • $begingroup$
          Thanks, I have to think about it.
          $endgroup$
          – Ulrich Neumann
          2 days ago














        8












        8








        8





        $begingroup$

        Using ReplaceRepeated (//.) and pattern matching:



        input //. {{x___, 0, 1, y___} :> {x, 0.5, 1, y}, {x___, 1, 0, y___} :> {x, 1, 0.5, y}}



        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




        Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.






        share|improve this answer









        $endgroup$



        Using ReplaceRepeated (//.) and pattern matching:



        input //. {{x___, 0, 1, y___} :> {x, 0.5, 1, y}, {x___, 1, 0, y___} :> {x, 1, 0.5, y}}



        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




        Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        eyorbleeyorble

        6,6681 gold badge11 silver badges30 bronze badges




        6,6681 gold badge11 silver badges30 bronze badges















        • $begingroup$
          Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
          $endgroup$
          – Ulrich Neumann
          2 days ago






        • 1




          $begingroup$
          /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
          $endgroup$
          – eyorble
          2 days ago










        • $begingroup$
          Thanks, I have to think about it.
          $endgroup$
          – Ulrich Neumann
          2 days ago


















        • $begingroup$
          Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
          $endgroup$
          – Ulrich Neumann
          2 days ago






        • 1




          $begingroup$
          /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
          $endgroup$
          – eyorble
          2 days ago










        • $begingroup$
          Thanks, I have to think about it.
          $endgroup$
          – Ulrich Neumann
          2 days ago
















        $begingroup$
        Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
        $endgroup$
        – Ulrich Neumann
        2 days ago




        $begingroup$
        Tricky. I tried it similar inside Cases but only one substitution is made. What could be the reason? Thanks!
        $endgroup$
        – Ulrich Neumann
        2 days ago




        1




        1




        $begingroup$
        /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
        $endgroup$
        – eyorble
        2 days ago




        $begingroup$
        /. only tries to replace once per complete match, as I understand, so since each list matches {x___, 1, 0, y___} or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.
        $endgroup$
        – eyorble
        2 days ago












        $begingroup$
        Thanks, I have to think about it.
        $endgroup$
        – Ulrich Neumann
        2 days ago




        $begingroup$
        Thanks, I have to think about it.
        $endgroup$
        – Ulrich Neumann
        2 days ago













        8












        $begingroup$

        You can also use a combination of SequenceReplace and FixedPoint:



        f = Map[SequenceReplace[{{0, 1} -> Sequence[.5, 1], {1, 0} -> Sequence[1, .5]}]], 

        FixedPoint[f, input]



        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
        0.5, 1, 0.5, 1},

        {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
        1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},

        {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
        0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




        You can also use Nest in place of FixedPoint:



        Nest[f , input, 2] == %



        True







        share|improve this answer











        $endgroup$




















          8












          $begingroup$

          You can also use a combination of SequenceReplace and FixedPoint:



          f = Map[SequenceReplace[{{0, 1} -> Sequence[.5, 1], {1, 0} -> Sequence[1, .5]}]], 

          FixedPoint[f, input]



          {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
          0.5, 1, 0.5, 1},

          {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
          1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},

          {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
          0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




          You can also use Nest in place of FixedPoint:



          Nest[f , input, 2] == %



          True







          share|improve this answer











          $endgroup$


















            8












            8








            8





            $begingroup$

            You can also use a combination of SequenceReplace and FixedPoint:



            f = Map[SequenceReplace[{{0, 1} -> Sequence[.5, 1], {1, 0} -> Sequence[1, .5]}]], 

            FixedPoint[f, input]



            {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
            0.5, 1, 0.5, 1},

            {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
            1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},

            {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
            0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




            You can also use Nest in place of FixedPoint:



            Nest[f , input, 2] == %



            True







            share|improve this answer











            $endgroup$



            You can also use a combination of SequenceReplace and FixedPoint:



            f = Map[SequenceReplace[{{0, 1} -> Sequence[.5, 1], {1, 0} -> Sequence[1, .5]}]], 

            FixedPoint[f, input]



            {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
            0.5, 1, 0.5, 1},

            {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
            1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},

            {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
            0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}




            You can also use Nest in place of FixedPoint:



            Nest[f , input, 2] == %



            True








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            kglrkglr

            211k10 gold badges242 silver badges485 bronze badges




            211k10 gold badges242 silver badges485 bronze badges


























                4












                $begingroup$

                For a rewriting problem, use rewriting explicitly:



                input //. {
                {x___, 0, 1, y___} -> {x, 0.5, 1, y},
                {x___, 1, 0, y___} -> {x, 1, 0.5, y}
                }
                (*
                {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}
                *)





                share|improve this answer









                $endgroup$




















                  4












                  $begingroup$

                  For a rewriting problem, use rewriting explicitly:



                  input //. {
                  {x___, 0, 1, y___} -> {x, 0.5, 1, y},
                  {x___, 1, 0, y___} -> {x, 1, 0.5, y}
                  }
                  (*
                  {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                  {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                  {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}
                  *)





                  share|improve this answer









                  $endgroup$


















                    4












                    4








                    4





                    $begingroup$

                    For a rewriting problem, use rewriting explicitly:



                    input //. {
                    {x___, 0, 1, y___} -> {x, 0.5, 1, y},
                    {x___, 1, 0, y___} -> {x, 1, 0.5, y}
                    }
                    (*
                    {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                    {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                    {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}
                    *)





                    share|improve this answer









                    $endgroup$



                    For a rewriting problem, use rewriting explicitly:



                    input //. {
                    {x___, 0, 1, y___} -> {x, 0.5, 1, y},
                    {x___, 1, 0, y___} -> {x, 1, 0.5, y}
                    }
                    (*
                    {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                    {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1},
                    {1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}
                    *)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 days ago









                    John DotyJohn Doty

                    8,7661 gold badge14 silver badges26 bronze badges




                    8,7661 gold badge14 silver badges26 bronze badges


























                        0












                        $begingroup$

                        This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show



                        foo[input_List] := Module[{n, i, current, next, before},
                        n = Length[input];

                        (*handle edge cases*)
                        If[n == 1, Return[input, Module]];
                        If[n == 2,
                        Return[If[input[[1]] == 0 && input[[2]] == 1, {0.5, 1},
                        If[input[[2]] == 0 && input[[1]] == 1, {1, 0.5}, input]],
                        Module]];

                        (*general case for list of length [GreaterEqual] 3*)
                        Table[
                        current = input[[i]];
                        If[i == 1,
                        If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
                        ,
                        If[i == n,
                        If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
                        ,
                        before = input[[i - 1]];
                        next = input[[i + 1]];
                        If[current == 0 && next == 1, 0.5,
                        If[current == 0 && before == 1, 0.5, current]]
                        ]
                        ],
                        {i, 1, Length[input]}
                        ]
                        ]


                        input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
                        0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
                        1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
                        0, 1, 0, 1}};


                        Now map foo on the input



                         result = foo[#] & /@ input


                        gives



                        {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 
                        0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
                        1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
                        0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}


                        I suspect there might be a shorter way to do this if one works harder on it.






                        share|improve this answer











                        $endgroup$




















                          0












                          $begingroup$

                          This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show



                          foo[input_List] := Module[{n, i, current, next, before},
                          n = Length[input];

                          (*handle edge cases*)
                          If[n == 1, Return[input, Module]];
                          If[n == 2,
                          Return[If[input[[1]] == 0 && input[[2]] == 1, {0.5, 1},
                          If[input[[2]] == 0 && input[[1]] == 1, {1, 0.5}, input]],
                          Module]];

                          (*general case for list of length [GreaterEqual] 3*)
                          Table[
                          current = input[[i]];
                          If[i == 1,
                          If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
                          ,
                          If[i == n,
                          If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
                          ,
                          before = input[[i - 1]];
                          next = input[[i + 1]];
                          If[current == 0 && next == 1, 0.5,
                          If[current == 0 && before == 1, 0.5, current]]
                          ]
                          ],
                          {i, 1, Length[input]}
                          ]
                          ]


                          input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
                          0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
                          1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
                          0, 1, 0, 1}};


                          Now map foo on the input



                           result = foo[#] & /@ input


                          gives



                          {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 
                          0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
                          1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
                          0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}


                          I suspect there might be a shorter way to do this if one works harder on it.






                          share|improve this answer











                          $endgroup$


















                            0












                            0








                            0





                            $begingroup$

                            This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show



                            foo[input_List] := Module[{n, i, current, next, before},
                            n = Length[input];

                            (*handle edge cases*)
                            If[n == 1, Return[input, Module]];
                            If[n == 2,
                            Return[If[input[[1]] == 0 && input[[2]] == 1, {0.5, 1},
                            If[input[[2]] == 0 && input[[1]] == 1, {1, 0.5}, input]],
                            Module]];

                            (*general case for list of length [GreaterEqual] 3*)
                            Table[
                            current = input[[i]];
                            If[i == 1,
                            If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
                            ,
                            If[i == n,
                            If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
                            ,
                            before = input[[i - 1]];
                            next = input[[i + 1]];
                            If[current == 0 && next == 1, 0.5,
                            If[current == 0 && before == 1, 0.5, current]]
                            ]
                            ],
                            {i, 1, Length[input]}
                            ]
                            ]


                            input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
                            0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
                            1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
                            0, 1, 0, 1}};


                            Now map foo on the input



                             result = foo[#] & /@ input


                            gives



                            {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 
                            0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
                            1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
                            0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}


                            I suspect there might be a shorter way to do this if one works harder on it.






                            share|improve this answer











                            $endgroup$



                            This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show



                            foo[input_List] := Module[{n, i, current, next, before},
                            n = Length[input];

                            (*handle edge cases*)
                            If[n == 1, Return[input, Module]];
                            If[n == 2,
                            Return[If[input[[1]] == 0 && input[[2]] == 1, {0.5, 1},
                            If[input[[2]] == 0 && input[[1]] == 1, {1, 0.5}, input]],
                            Module]];

                            (*general case for list of length [GreaterEqual] 3*)
                            Table[
                            current = input[[i]];
                            If[i == 1,
                            If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
                            ,
                            If[i == n,
                            If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
                            ,
                            before = input[[i - 1]];
                            next = input[[i + 1]];
                            If[current == 0 && next == 1, 0.5,
                            If[current == 0 && before == 1, 0.5, current]]
                            ]
                            ],
                            {i, 1, Length[input]}
                            ]
                            ]


                            input = {{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
                            0, 1}, {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
                            1, 0, 1}, {1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
                            0, 1, 0, 1}};


                            Now map foo on the input



                             result = foo[#] & /@ input


                            gives



                            {{0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 
                            0.5, 1, 0.5, 1}, {0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
                            1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}, {1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
                            0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1}}


                            I suspect there might be a shorter way to do this if one works harder on it.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 2 days ago

























                            answered 2 days ago









                            NasserNasser

                            61.3k4 gold badges93 silver badges215 bronze badges




                            61.3k4 gold badges93 silver badges215 bronze badges

































                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Mathematica Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                Use MathJax to format equations. MathJax reference.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f203760%2flist-map-function-based-on-a-condition%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...