Re: Efficient way to merge lists--Programming help
- To: mathgroup at smc.vnet.net
- Subject: [mg12722] Re: Efficient way to merge lists--Programming help
- From: Julian Stoev <stoev at SPAM-RE-MO-VER-usa.net>
- Date: Thu, 4 Jun 1998 02:52:08 -0400
- Organization: Seoul National University, Republic of Korea
- References: <6l2r6l$e3p@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 3 Jun 1998, Joel Cannon wrote:
|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 think you may try to use pattern matching.
For example:
t2=test2/.{x_,y_,z_}->Rule[{x,y,_},{x,y,z}]
Now you have transformation rules based on test2 and you can apply them
to test
test/.t2
This is working with any dimensions of test variable.
I generated an example using some data:
In[115]:=
test2=Table[{i,2,i*3},{i,1,4}]
rules=test2/.{x_,y_,z_}->Rule[{x,y,_},{x,y,z}]
Out[115]=
{{1, 2, 3}, {2, 2, 6}, {3, 2, 9}, {4, 2, 12}} Out[116]=
{{1, 2, _} -> {1, 2, 3}, {2, 2, _} -> {2, 2, 6},
{3, 2, _} -> {3, 2, 9}, {4, 2, _} -> {4, 2, 12}}
In[117]:=
test=Table[{i,j,i*j},{i,1,4},{j,1,3}] test/.rules
Out[117]=
{{{1, 1, 1}, {1, 2, 2}, {1, 3, 3}},
{{2, 1, 2}, {2, 2, 4}, {2, 3, 6}},
{{3, 1, 3}, {3, 2, 6}, {3, 3, 9}},
{{4, 1, 4}, {4, 2, 8}, {4, 3, 12}}} Out[118]=
{{{1, 1, 1}, {1, 2, 3}, {1, 3, 3}},
{{2, 1, 2}, {2, 2, 6}, {2, 3, 6}},
{{3, 1, 3}, {3, 2, 9}, {3, 3, 9}},
{{4, 1, 4}, {4, 2, 12}, {4, 3, 12}}}
There may be some enhancements, but you have the general idea, I hope.
--Julian Stoev