Re: Need to reduce 2 lists so that only {x,y} pairs with same x remain
- To: mathgroup at smc.vnet.net
- Subject: [mg24186] Re: [mg24134] Need to reduce 2 lists so that only {x,y} pairs with same x remain
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Wed, 28 Jun 2000 22:51:07 -0400 (EDT)
- References: <200006280611.CAA13359@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
materialsscientist at my-deja.com wrote:
>
> Hello,
>
> I have 2 lists of data:
>
> for example
> data1={{1,1},{1.5,1.5},{2,2},{3,3},{4,4},{5,5}}
>
> data2={{1,2},{2,3},{3,4},{4,5},{6,7},{7,8}}
>
> I want to do a point by point comparision, so I
> need to reduce both data1 and data2 so that
> common x data in the {x,y} pairs for each list
> remain.
>
> Thus the lists should look like this:
>
> data1={{1,1},{2,2},{3,3},{4,4}}
> data2={{1,2},{2,3},{3,4},{4,5}}
>
> What function(s) would to something like this?
>
> Thanks,
>
> Chuck
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
This will work.
In[9]:= d1a = Complement[data1, data2, SameTest->(#1[[1]]==#2[[1]]&)];
In[10]:= d1b = Complement[data1, d1a]
Out[10]= {{1, 1}, {2, 2}, {3, 3}, {4, 4}}
Do similarly to get the reduced version of data2.
A more efficient method:
In[11]:= d1c = Intersection[data1, data2, SameTest->(#1[[1]]==#2[[1]]&)]
Out[11]= {{1, 1}, {2, 2}, {3, 3}, {4, 4}}
In[12]:= d2c = Intersection[data2, data1, SameTest->(#1[[1]]==#2[[1]]&)]
Out[12]= {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
A potential problem is that I'm not sure the language semantics
guarantee that this will always work, if, for example, internals of
Intersection implementation are changed.
Daniel Lichtblau
Wolfram Research
- References:
- Need to reduce 2 lists so that only {x,y} pairs with same x remain
- From: materialsscientist@my-deja.com
- Need to reduce 2 lists so that only {x,y} pairs with same x remain