How to replace an element in a list with an element in another list with matching indices - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-17T12:14:47Z https://mathematica.stackexchange.com/feeds/question/95484 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/95484 5 How to replace an element in a list with an element in another list with matching indices BetterEnglish https://mathematica.stackexchange.com/users/11065 2015-09-25T15:30:30Z 2016-01-31T06:06:45Z <p>I have a list like this <code>l = {1 + 2*s, 2 + 3*s, 4 + 1*s}</code>, and I have calculated <code>s = {1, 2, 3}</code>. I need to replace <code>s</code> in <code>l</code> so that <code>l = {1 + 2*1, 2 + 3*2, 4 + 1*3}</code>. Is it possible to do that with <code>ReplacAll</code> (<code>/.</code>) and <code>Rule</code>?</p> https://mathematica.stackexchange.com/questions/95484/-/95488#95488 7 Answer by Bob Hanlon for How to replace an element in a list with an element in another list with matching indices Bob Hanlon https://mathematica.stackexchange.com/users/9362 2015-09-25T15:53:47Z 2015-09-25T15:53:47Z <pre><code>list={1+2*s,2+3*s,4+1*s}; sValues={1,2,3}; </code></pre> <p>Using <code>HoldForm</code> to see the intermediate step</p> <pre><code>Inner[HoldForm[#1 /. s -&gt; #2]&amp;, list, sValues, List] (* {1+2 s/. s-&gt;1,2+3 s/. s-&gt;2,4+s/. s-&gt;3} *) %//ReleaseHold (* {3,8,7} *) </code></pre> <p>Without the intermediate step</p> <pre><code>Inner[#1 /. s -&gt; #2 &amp;, list, sValues, List] (* {3,8,7} *) </code></pre> https://mathematica.stackexchange.com/questions/95484/-/95489#95489 1 Answer by BetterEnglish for How to replace an element in a list with an element in another list with matching indices BetterEnglish https://mathematica.stackexchange.com/users/11065 2015-09-25T15:56:07Z 2015-09-25T15:56:07Z <pre><code>{#1[] /. s -&gt; #2[], #1[] /. s -&gt; #2[], #1[] /. s -&gt; #2[]} &amp;[{1+2*s,2+3*s,4+1*s}, {1,2,3}] </code></pre> https://mathematica.stackexchange.com/questions/95484/-/95497#95497 7 Answer by march for How to replace an element in a list with an element in another list with matching indices march https://mathematica.stackexchange.com/users/29734 2015-09-25T17:17:56Z 2016-01-31T06:06:45Z <p>Here's some ways. Using:</p> <pre><code>l = {1+2*s,2+3*s,4+1*s}; slist = {1, 2, 3}; </code></pre> <p>The answer closest to what you asked is</p> <pre><code>{1 + 2*s, 2 + 3*s, 4 + 1*s} /. s -&gt; slist // Diagonal </code></pre> <p>However, that does <code>3^2 - 3</code> too many calculations, so here's some more ways.</p> <pre><code>Module[{i = 1}, l /. s :&gt; slist[[i++]]] MapThread[#1 /. s -&gt; #2 &amp;, {l, slist}] MapThread[ReplaceAll, {l, Thread[s -&gt; slist]}] ReplaceAll @@@ Transpose[{l, Thread[s -&gt; slist]}] Map[First@#1 /. s -&gt; Last@#1 &amp;, Transpose@{l, slist}] MapIndexed[#1 /. s -&gt; slist[[First@#2]] &amp;, l] </code></pre> <p>If <code>s</code> is always going to be the list <code>{1, 2, 3, ...}</code>, then:</p> <pre><code>MapIndexed[#1 /. s -&gt; First@#2 &amp;, l] </code></pre>