Re: element selection

*To*: mathgroup at smc.vnet.net*Subject*: [mg53632] Re: [mg53605] element selection*From*: "David Park" <djmp at earthlink.net>*Date*: Fri, 21 Jan 2005 06:35:38 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Peter, Inner is always a good command to consider when performing operations on the elements of two equal length lists. list1 = {1, 4, 5, 10, 11}; list2 = {1, 2, 4, 7, 9}; Inner[If[#1 - #2 < 2, {#1, #2}, Unevaluated[Sequence[]]] &, list1, list2, List] Here if the difference of the two elements from each of the lists is less than 2 we form the couple, otherwise we put in an empty Sequence. (Sequence[] must be Unevaluated, otherwise it will disappear from the If statement rather than the resulting list.) The resulting elements are wrapped in List as specified by the 4th argument of Inner. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Bruyndonckx P. [mailto:pbruynd at vub.ac.be] To: mathgroup at smc.vnet.net I have two long sorted lists of Integers. If the difference between a number in list 1 and a number in list 2 is less than a certain value, both are selected to create a couple. I am looking for a fast way to go through both lists and extract these elements to create a new list of couples. I tried a number of things, but all of them were rather slow so I need some expert advice here. Thx, Peter