Re: Re: Replace specific element with specific value.
- To: mathgroup at smc.vnet.net
- Subject: [mg99732] Re: [mg99679] Re: Replace specific element with specific value.
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Tue, 12 May 2009 03:11:01 -0400 (EDT)
- Reply-to: hanlonr at cox.net
myReplacePart[data_List, pos_List, parts_List] :=
Module[{newList = data},
( newList = ReplacePart[newList, #[[1]] -> #[[2]]]) & /@
Transpose[{Thread[{pos, 2}], parts}];
newList] /; Length[pos] == Length[parts]
data = {{0, 2}, {2, 3}, {3, 4}, {4, 6}, {6, 8}, {8, 9}, {9, 10}};
positionList = {3, 6, 7};
replacementList = {8, 17, 25};
myReplacePart[data, positionList, replacementList]
{{0, 2}, {2, 3}, {3, 8}, {4, 6}, {6, 8}, {8, 17}, {9, 25}}
Bob Hanlon
---- nick.maj at mssm.edu wrote:
=============
Thank you all for the replies.
Bill, you are right. To manually input the replacement rules is fine
for only a view variables. For simplicity I kept my example short.
However, I am dealing with quite large lists (100+), so a functional/
programatical approach is necessary. Also, I used characters for
clarity.
Rule @@@ (ToExpression[{#, # <> "+" <> # <> #}] & /@ {"1", "2", "3"})
gives: {1 -> 12, 2 -> 24, 3 -> 36}
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.
I experimented with MapThread[ReplacePart ... but without success.
Thanks in advance, Nick.