Re: Correlating two lists

• To: mathgroup at smc.vnet.net
• Subject: [mg100238] Re: Correlating two lists
• From: Pillsy <pillsbury at gmail.com>
• Date: Thu, 28 May 2009 19:35:28 -0400 (EDT)
• References: <gv2jr1\$9af\$1@smc.vnet.net> <gvhm8r\$82s\$1@smc.vnet.net>

```On May 26, 5:17 pm, anguz... at ing.uchile.cl wrote:
[...]
> Two lists: "x" and "y";  and a boolean relation R[a,b] with a in x and
> b in y, and for each "a" there is at most one "b" in "y" such that
> R[xi,yj]is true.
> R is also "expensive" to calculate. I would like to group each xi in
> "x" with its partner in "y" (if there is one) and create a merged list:
> {{x1,y1},{x2},{x3},{x4,y4},...}
> such that R[xi,yi]  is true
> I'm looking for the adequate Mathematica function to do this as
> efficiently as possible. by the way,

This function should do the trick. It allows you to pass in whatever
lists x and y that you like, and any binary relation R as well.

merge[x_, y_, R_] := Function[xi, Prepend[Select[y, R[xi, #1] & , 1],
xi]] /@ x

Using the optional last argument for functions like Select (or Cases,
Position, et c.) allows you to efficiently handle those cases where
you only need the first match. Generally speaking, you will get a lot
of mileage out of recasting things in terms of patterns and/or pure
functions.

Cheers,
Pillsy

```

• Prev by Date: Re: Tube in ParametricPlot3D with too many details
• Next by Date: Re: Working with Indeterminate in large numerical lists
• Previous by thread: Re: Correlating two lists
• Next by thread: Re: list manipulation