RE: Need to reduce 2 lists so that only {x,y} pairs with same x remain

• To: mathgroup at smc.vnet.net
• Subject: [mg24192] RE: [mg24134] Need to reduce 2 lists so that only {x,y} pairs with same x remain
• From: "David Park" <djmp at earthlink.net>
• Date: Wed, 28 Jun 2000 22:51:12 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: materialsscientist at my-deja.com
To: mathgroup at smc.vnet.net
> [mailto:materialsscientist at my-deja.com]
> 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

Chuck,

I hope you get a number of responses to this. Here is a method I worked out.
We build up a list of "outs" by taking the Complement of the first list with
the second list and then vice versa. Then we Map the Complement of the outs
on each of the two lists. We have to specify a SameTest for the Complement
function. We want the sublists to be the same if their first element is the
same. However, I suspect that your data may be more complicated than you
indicate. How closely must the first elements match to keep them? In the
following routine, if they differ by less than delta they are kept.

matchlists[list1_, list2_, delta_] :=
Module[{outs, lista = Sort[list1], listb = Sort[list2]},
outs = Join[Complement[lista, listb, SameTest ->
(Abs[#1[[1]] - #2[[1]]] < delta & )], Complement[listb, lista,
SameTest -> (Abs[#1[[1]] - #2[[1]]] < delta & )]];
(Complement[#1, outs] & ) /@ {lista, listb}]

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}};

matchlists[data1, data2, 0.001]
{{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 2}, {2, 3}, {3, 4}, {4, 5}}}

data1 = {{1.11135, 1}, {1.5, 1.5}, {2.2255, 2}, {3, 3}, {4, 4}, {5, 5}};
data2 = {{1.11105, 2}, {2.2333, 3}, {3, 4}, {4, 5}, {6, 7}, {7, 8}};

matchlists[data1, data2, 0.001]
{{{1.11135, 1}, {3, 3}, {4, 4}}, {{1.11105, 2}, {3, 4}, {4, 5}}}

David Park