I have two lists of complex numbers,
list2, sorted by decreasing imaginary part.
list2 is strictly longer than
Most numbers of
list1 will be different than those in
list2, but some will be close.
I want to find the intersection of these lists, where two complex numbers are considered equal if they differ in absolute value by at most
The built-in way to do this would be
intersection = Intersection[list1,list2,SameTest->(Abs[#1-#2]<10^-4&)];
However the intersection I want is different from this:
- The order should stay fixed
- Duplicates within one list should not be removed
- By default the intersection should be taken from
list1, with the option of also giving a second list which is the intersection with elements taken from
- This second intersection should differ from the first only in the order
10^-ndifferences in the elements, not in length or ordering.
A simple but slow way of doing this would be:
intersection1 = Select[list1,Min@Abs[#-list2]<10^-4&]; intersection2 = Function[el1,MinimalBy[list2,Abs[#-el1]&][]]/@intersection1;
Note that if we did the
intersection2 the same way as
intersection1, it would likely contain more elements, as
list2 is longer than
list2 may contain several points close to one point in
There must be a more elegant and quick way though, at the very least this is doing the work twice.
Typical list lengths are from 100 to 1000 elements, around 10.000 at the very max, so speed is important but not critical, I'm mostly just curious how to do this properly in Mathematica.