Re: Efficient way to merge lists--Programming help
- To: mathgroup at smc.vnet.net
- Subject: [mg12725] Re: Efficient way to merge lists--Programming help
- From: "Allan Hayes" <hay at haystack.demon.cc.uk>
- Date: Thu, 4 Jun 1998 02:52:11 -0400
- References: <6l2r6l$e3p@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Joel Cannon wrote in message <6l2r6l$e3p at smc.vnet.net>... >Cam someone suggest better ways to accomplish the following? > >I have two lists containing triplets--test is a rectangular array of >triplets {x,y,z} (e.g. Dimensions {4,40,3}) and test2 is an array of >triplets (e.g. Dimensions {50,3}). test2 is to replace equivalent >elements in test (when the x and y values are equal--the test2 z values >have been calculated more accurately). > >The task can be understood to be: > >1. Given a triplet {x_i,y_i,z_i} from test2, find the position in test >which has the same {x_i,y_i,_}. > >2. Replace the element in test by the triplet from test2. > > >I resorted to a loop after I decided that it would take me too much time >to figure out a better, more elegant solution. Here is what I did: > >For[i=1,i<=Length[test2],i++, > test = ReplacePart[test,test2[[i]], > test//Position[#,{test2[[i]][[1]],test2[[i]][[2]],_}]& //Flatten]]; > I suspect that the following may be slower, but its different. test={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; test2={{2,3,-4},{4,5,-6}}; test/.(test2/.{x_?NumericQ,y_,z_}-> ({x,y,_}->{x,y,z})) {{1,2,3},{2,3,-4},{3,4,5},{4,5,-6}} ------------------------------------------------------------- Allan Hayes Training and Consulting Leicester UK http://www.haystack.demon.co.uk hay at haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44(0)116 271 8642