Re: Replace specific element with specific value.
- To: mathgroup at smc.vnet.net
- Subject: [mg99723] Re: Replace specific element with specific value.
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 12 May 2009 03:09:22 -0400 (EDT)
On 5/11/09 at 6:22 AM, nick.maj at mssm.edu wrote: >Let me elaborate on this problem again with numbers only. >data = {{0, 2}, {2, 3}, {3, 4}, {4, 6}, {6, 8}, {8, 9}, {9, 10}} >positionList = {3, 6, 7} >replacementList = {8, 17, 25} >needed output: >{{0, 2}, {2, 3}, {3, 8}, {4, 6}, {6, 8}, {8, 17}, {9, 25}} >Based on the positionList - replacementList information, the >function must replace the 2nd element in the 3rd, 6th and 7th list >of data with the value 8, 17 and 25 respectively. Here is one way: In[6]:= ReplacePart[data, Thread[Evaluate[{#, 2} & /@ positionList] -> replacementList]] Out[6]= {{0, 2}, {2, 3}, {3, 8}, {4, 6}, {6, 8}, {8, 17}, {9, 25}} Note, the output is what you want and the original list is unchanged. That is: In[7]:= data Out[7]= {{0, 2}, {2, 3}, {3, 4}, {4, 6}, {6, 8}, {8, 9}, {9, 10}} Obviously, if you want the original list changed all you need do is set the original list to the output.