Skip to main content
added 2 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Part II

Part II

Given the comments below, clearly Intersection isn't going to give me what I want, which is

  1. every element from list a that is within some given distance of any element within list b. and
  2. every element from list b that is within some given distance of any element within list a.

I can do this with something ugly like...

Union[Flatten[Select[Apply[Join, Outer[List, a, b]], Abs[{1, -1}.#] <= 1 &]]] 

But the Outer is likely to bite hard with big lists.

Any improvements spring to mind?

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Part II

Given the comments below, clearly Intersection isn't going to give me what I want, which is

  1. every element from list a that is within some given distance of any element within list b. and
  2. every element from list b that is within some given distance of any element within list a.

I can do this with something ugly like...

Union[Flatten[Select[Apply[Join, Outer[List, a, b]], Abs[{1, -1}.#] <= 1 &]]] 

But the Outer is likely to bite hard with big lists.

Any improvements spring to mind?

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Part II

Given the comments below, clearly Intersection isn't going to give me what I want, which is

  1. every element from list a that is within some given distance of any element within list b. and
  2. every element from list b that is within some given distance of any element within list a.

I can do this with something ugly like...

Union[Flatten[Select[Apply[Join, Outer[List, a, b]], Abs[{1, -1}.#] <= 1 &]]] 

But the Outer is likely to bite hard with big lists.

Any improvements spring to mind?

Additional specification of problem and initial solution
Source Link
Ymareth
  • 4.8k
  • 22
  • 29

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Part II

Given the comments below, clearly Intersection isn't going to give me what I want, which is

  1. every element from list a that is within some given distance of any element within list b. and
  2. every element from list b that is within some given distance of any element within list a.

I can do this with something ugly like...

Union[Flatten[Select[Apply[Join, Outer[List, a, b]], Abs[{1, -1}.#] <= 1 &]]] 

But the Outer is likely to bite hard with big lists.

Any improvements spring to mind?

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Part II

Given the comments below, clearly Intersection isn't going to give me what I want, which is

  1. every element from list a that is within some given distance of any element within list b. and
  2. every element from list b that is within some given distance of any element within list a.

I can do this with something ugly like...

Union[Flatten[Select[Apply[Join, Outer[List, a, b]], Abs[{1, -1}.#] <= 1 &]]] 

But the Outer is likely to bite hard with big lists.

Any improvements spring to mind?

Typo fixed, 2 in wrong place.
Source Link
Ymareth
  • 4.8k
  • 22
  • 29

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#1]<=2&#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#1]<=2&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Intersection works as expected in this case...

a = Range[1,5];b=Range[3,7]; Intersection[a,b] 

giving...

{3,4,5} 

However if I expand the concept of sameness using SameTest to this...

Intersection[a,b, SameTest->(Abs[#1-#2]<=1&)] 

I get the slightly puzzling result of...

{5} 

I was expecting to see something like {2,3,4,5,6}.

I thought this might be Union running within Intersection but...

Union[{2,3,4,5,6},SameTest->(Abs[#1-#2]<=1&)] 

gives...

{2,4,6} 

So I am at something of a loss.

Source Link
Ymareth
  • 4.8k
  • 22
  • 29
Loading