Replacing the Zeros of One List by the (i-1)th Element of Another List

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5












$begingroup$


My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if



list1 = 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0


and



list2 = 6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12


the desired output is 0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0. Note that the first element of the output is defined as 0 still.



My attempt to create a code for this is to first find the zeros of list1then run a for-loop for $i in$zeroslist1.



zeroslist1 = Flatten[Position[list1, 0]]
DeleteCases[
Flatten[Reap@
Do[Sow[ReplacePart[vtest1,
i -> vtest2[[i - 1]] & /@ zeroslist1]], i, zeroslist1], 2], Null]


The results of the output are:



List,1,1,0,0,1,1,0,1,0,0,1,0, 0,1,1,4,0,1,1,0,1,0,0,1,0, 0,1,1,0,7,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,10,1,0,0,1,0, 0,1,1,0,0,1,1,0,1,11,0,1,0,0,1,1,0,0,1,1,0,1,0,3,1,0, 0,1,1,0,0,1,1,0,1,0,0,1,0.



Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.










share|improve this question









$endgroup$
















    5












    $begingroup$


    My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if



    list1 = 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0


    and



    list2 = 6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12


    the desired output is 0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0. Note that the first element of the output is defined as 0 still.



    My attempt to create a code for this is to first find the zeros of list1then run a for-loop for $i in$zeroslist1.



    zeroslist1 = Flatten[Position[list1, 0]]
    DeleteCases[
    Flatten[Reap@
    Do[Sow[ReplacePart[vtest1,
    i -> vtest2[[i - 1]] & /@ zeroslist1]], i, zeroslist1], 2], Null]


    The results of the output are:



    List,1,1,0,0,1,1,0,1,0,0,1,0, 0,1,1,4,0,1,1,0,1,0,0,1,0, 0,1,1,0,7,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,10,1,0,0,1,0, 0,1,1,0,0,1,1,0,1,11,0,1,0,0,1,1,0,0,1,1,0,1,0,3,1,0, 0,1,1,0,0,1,1,0,1,0,0,1,0.



    Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.










    share|improve this question









    $endgroup$














      5












      5








      5





      $begingroup$


      My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if



      list1 = 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0


      and



      list2 = 6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12


      the desired output is 0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0. Note that the first element of the output is defined as 0 still.



      My attempt to create a code for this is to first find the zeros of list1then run a for-loop for $i in$zeroslist1.



      zeroslist1 = Flatten[Position[list1, 0]]
      DeleteCases[
      Flatten[Reap@
      Do[Sow[ReplacePart[vtest1,
      i -> vtest2[[i - 1]] & /@ zeroslist1]], i, zeroslist1], 2], Null]


      The results of the output are:



      List,1,1,0,0,1,1,0,1,0,0,1,0, 0,1,1,4,0,1,1,0,1,0,0,1,0, 0,1,1,0,7,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,10,1,0,0,1,0, 0,1,1,0,0,1,1,0,1,11,0,1,0,0,1,1,0,0,1,1,0,1,0,3,1,0, 0,1,1,0,0,1,1,0,1,0,0,1,0.



      Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.










      share|improve this question









      $endgroup$




      My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if



      list1 = 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0


      and



      list2 = 6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12


      the desired output is 0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0. Note that the first element of the output is defined as 0 still.



      My attempt to create a code for this is to first find the zeros of list1then run a for-loop for $i in$zeroslist1.



      zeroslist1 = Flatten[Position[list1, 0]]
      DeleteCases[
      Flatten[Reap@
      Do[Sow[ReplacePart[vtest1,
      i -> vtest2[[i - 1]] & /@ zeroslist1]], i, zeroslist1], 2], Null]


      The results of the output are:



      List,1,1,0,0,1,1,0,1,0,0,1,0, 0,1,1,4,0,1,1,0,1,0,0,1,0, 0,1,1,0,7,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,10,1,0,0,1,0, 0,1,1,0,0,1,1,0,1,11,0,1,0,0,1,1,0,0,1,1,0,1,0,3,1,0, 0,1,1,0,0,1,1,0,1,0,0,1,0.



      Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.







      list-manipulation functions function-construction






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 11 at 17:03









      WillWill

      3348




      3348




















          5 Answers
          5






          active

          oldest

          votes


















          6












          $begingroup$

          You can use a multiplication instead of looping or conditionals:



          list1 + (1 - list1) Prepend[Most[list2], 0]



          0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




          The central point is that 0 and 1 in list1 aren't just symbols but numeric quantities.






          share|improve this answer











          $endgroup$












          • $begingroup$
            What a simple way to do this. Thank you so much.
            $endgroup$
            – Will
            Feb 11 at 17:51


















          4












          $begingroup$

          idx = Random`Private`PositionsOf[Rest[list1], 0];
          result = list1;
          result[[idx + 1]] = list2[[idx]];
          result



          0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







          share|improve this answer









          $endgroup$




















            3












            $begingroup$

            Here's another possibility:



            Module[tmp=list1,
            With[i = Pick[Range[Length[list1]-1], Rest @ list1, 0],
            tmp[[i+1]]=list2[[i]]
            ];
            tmp
            ]



            0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







            share|improve this answer









            $endgroup$




















              2












              $begingroup$

              Also MapIndexed works fine



              (# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]


              The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head, which in this case is List). Then the function MapIndexed does its job.






              share|improve this answer









              $endgroup$




















                0












                $begingroup$

                MapThread[If[#1 == 0, #2, #1] &, Join[list1, 0], Join[0, list2]] // Most



                0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                Your answer from above




                0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                share|improve this answer









                $endgroup$












                  Your Answer





                  StackExchange.ifUsing("editor", function ()
                  return StackExchange.using("mathjaxEditing", function ()
                  StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                  StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                  );
                  );
                  , "mathjax-editing");

                  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%2f191320%2freplacing-the-zeros-of-one-list-by-the-i-1th-element-of-another-list%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  6












                  $begingroup$

                  You can use a multiplication instead of looping or conditionals:



                  list1 + (1 - list1) Prepend[Most[list2], 0]



                  0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                  The central point is that 0 and 1 in list1 aren't just symbols but numeric quantities.






                  share|improve this answer











                  $endgroup$












                  • $begingroup$
                    What a simple way to do this. Thank you so much.
                    $endgroup$
                    – Will
                    Feb 11 at 17:51















                  6












                  $begingroup$

                  You can use a multiplication instead of looping or conditionals:



                  list1 + (1 - list1) Prepend[Most[list2], 0]



                  0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                  The central point is that 0 and 1 in list1 aren't just symbols but numeric quantities.






                  share|improve this answer











                  $endgroup$












                  • $begingroup$
                    What a simple way to do this. Thank you so much.
                    $endgroup$
                    – Will
                    Feb 11 at 17:51













                  6












                  6








                  6





                  $begingroup$

                  You can use a multiplication instead of looping or conditionals:



                  list1 + (1 - list1) Prepend[Most[list2], 0]



                  0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                  The central point is that 0 and 1 in list1 aren't just symbols but numeric quantities.






                  share|improve this answer











                  $endgroup$



                  You can use a multiplication instead of looping or conditionals:



                  list1 + (1 - list1) Prepend[Most[list2], 0]



                  0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                  The central point is that 0 and 1 in list1 aren't just symbols but numeric quantities.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 11 at 17:59

























                  answered Feb 11 at 17:32









                  RomanRoman

                  2,624717




                  2,624717











                  • $begingroup$
                    What a simple way to do this. Thank you so much.
                    $endgroup$
                    – Will
                    Feb 11 at 17:51
















                  • $begingroup$
                    What a simple way to do this. Thank you so much.
                    $endgroup$
                    – Will
                    Feb 11 at 17:51















                  $begingroup$
                  What a simple way to do this. Thank you so much.
                  $endgroup$
                  – Will
                  Feb 11 at 17:51




                  $begingroup$
                  What a simple way to do this. Thank you so much.
                  $endgroup$
                  – Will
                  Feb 11 at 17:51











                  4












                  $begingroup$

                  idx = Random`Private`PositionsOf[Rest[list1], 0];
                  result = list1;
                  result[[idx + 1]] = list2[[idx]];
                  result



                  0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                  share|improve this answer









                  $endgroup$

















                    4












                    $begingroup$

                    idx = Random`Private`PositionsOf[Rest[list1], 0];
                    result = list1;
                    result[[idx + 1]] = list2[[idx]];
                    result



                    0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                    share|improve this answer









                    $endgroup$















                      4












                      4








                      4





                      $begingroup$

                      idx = Random`Private`PositionsOf[Rest[list1], 0];
                      result = list1;
                      result[[idx + 1]] = list2[[idx]];
                      result



                      0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                      share|improve this answer









                      $endgroup$



                      idx = Random`Private`PositionsOf[Rest[list1], 0];
                      result = list1;
                      result[[idx + 1]] = list2[[idx]];
                      result



                      0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 11 at 17:12









                      Henrik SchumacherHenrik Schumacher

                      55.5k576154




                      55.5k576154





















                          3












                          $begingroup$

                          Here's another possibility:



                          Module[tmp=list1,
                          With[i = Pick[Range[Length[list1]-1], Rest @ list1, 0],
                          tmp[[i+1]]=list2[[i]]
                          ];
                          tmp
                          ]



                          0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                          share|improve this answer









                          $endgroup$

















                            3












                            $begingroup$

                            Here's another possibility:



                            Module[tmp=list1,
                            With[i = Pick[Range[Length[list1]-1], Rest @ list1, 0],
                            tmp[[i+1]]=list2[[i]]
                            ];
                            tmp
                            ]



                            0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                            share|improve this answer









                            $endgroup$















                              3












                              3








                              3





                              $begingroup$

                              Here's another possibility:



                              Module[tmp=list1,
                              With[i = Pick[Range[Length[list1]-1], Rest @ list1, 0],
                              tmp[[i+1]]=list2[[i]]
                              ];
                              tmp
                              ]



                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                              share|improve this answer









                              $endgroup$



                              Here's another possibility:



                              Module[tmp=list1,
                              With[i = Pick[Range[Length[list1]-1], Rest @ list1, 0],
                              tmp[[i+1]]=list2[[i]]
                              ];
                              tmp
                              ]



                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0








                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 11 at 17:13









                              Carl WollCarl Woll

                              69.3k393179




                              69.3k393179





















                                  2












                                  $begingroup$

                                  Also MapIndexed works fine



                                  (# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]


                                  The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head, which in this case is List). Then the function MapIndexed does its job.






                                  share|improve this answer









                                  $endgroup$

















                                    2












                                    $begingroup$

                                    Also MapIndexed works fine



                                    (# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]


                                    The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head, which in this case is List). Then the function MapIndexed does its job.






                                    share|improve this answer









                                    $endgroup$















                                      2












                                      2








                                      2





                                      $begingroup$

                                      Also MapIndexed works fine



                                      (# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]


                                      The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head, which in this case is List). Then the function MapIndexed does its job.






                                      share|improve this answer









                                      $endgroup$



                                      Also MapIndexed works fine



                                      (# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]


                                      The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head, which in this case is List). Then the function MapIndexed does its job.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 11 at 17:16









                                      Mane.andreaMane.andrea

                                      1816




                                      1816





















                                          0












                                          $begingroup$

                                          MapThread[If[#1 == 0, #2, #1] &, Join[list1, 0], Join[0, list2]] // Most



                                          0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                                          Your answer from above




                                          0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                                          share|improve this answer









                                          $endgroup$

















                                            0












                                            $begingroup$

                                            MapThread[If[#1 == 0, #2, #1] &, Join[list1, 0], Join[0, list2]] // Most



                                            0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                                            Your answer from above




                                            0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                                            share|improve this answer









                                            $endgroup$















                                              0












                                              0








                                              0





                                              $begingroup$

                                              MapThread[If[#1 == 0, #2, #1] &, Join[list1, 0], Join[0, list2]] // Most



                                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                                              Your answer from above




                                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0







                                              share|improve this answer









                                              $endgroup$



                                              MapThread[If[#1 == 0, #2, #1] &, Join[list1, 0], Join[0, list2]] // Most



                                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0




                                              Your answer from above




                                              0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0








                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Feb 11 at 17:12









                                              MikeYMikeY

                                              3,258614




                                              3,258614



























                                                  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%2f191320%2freplacing-the-zeros-of-one-list-by-the-i-1th-element-of-another-list%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

                                                  How to check contact read email or not when send email to Individual?

                                                  Displaying single band from multi-band raster using QGIS

                                                  How many registers does an x86_64 CPU actually have?