7

Please consider :

dalist = {{9, 6}, {5, 6}, {6, 0}, {0, 5}, {10, 8}, {1, 2}, {10, 4}, {1, 1}, {7, 7}, 
          {6, 8}, {5, 3}, {6, 10}, {7, 4}, {1, 8}, {10, 0}, {10, 7}, {6, 3}, {4, 0}, 
           {9, 2}, {4, 7}, {1, 6}, {10, 8}, {7, 8}, {0, 10}, {3, 4}, {0, 0}, {8, 5}, 
           {4, 5}, {6,0}, {2, 9}, {2, 4}, {8, 4}, {7, 4}, {3, 6}, {7, 10}, {1, 10}, 
           {1, 4}, {8, 0}, {8, 9}, {5, 4}, {2, 5}, {2, 9}, {3, 1}, {0, 6}, {10, 3}, 
           {9, 6}, {8, 7}, {7, 6}, {7, 3}, {8, 9}};

frameCenter = {5, 5};

criticalRadius = 2;

Graphics[{
          White, EdgeForm[Thick], Rectangle[{0, 0}, {10, 10}], Black,
          Point /@ dalist,
          Circle[frameCenter, 2]}];

enter image description here

I would like to create a test to go over dalist and reject the points that are located within or on a certain radius from the frameCenter as illustrated above. I have done that in the past with a rectangular zone but am puzzled on how to do so with a circular area

1

3 Answers 3

8

This will be pretty efficient:

In[82]:= 
Pick[dalist,UnitStep[criticalRadius^2-Total[(Transpose[dalist]-frameCenter)^2]],0]

Out[82]= 
{{6,0},{10,8},{10,4},{1,1},{6,10},{10,0},{10,7},{4,0},{10,8},
{0,10},{0,0},{6,0},{7,10},{1,10},{8,0},{0,6},{10,3}}

Alternatively,

In[86]:= Select[dalist, EuclideanDistance[#, frameCenter] > criticalRadius &]

Out[86]= {{6, 0}, {10, 8}, {10, 4}, {1, 1}, {6, 10}, {10, 0}, {10, 7}, {4, 0}, 
 {10, 8}, {0, 10}, {0, 0}, {6, 0}, {7, 10}, {1, 10}, {8, 0}, {0, 6}, {10, 3}}
7

Nearest can be useful for such purposes as finding every set member in some radius of a given point. One uses the less-than-well-documented form of third argument that allows a pair denoting {max number, max distance}. We allow in this case as many as will fit inside the max radius, so the max number is just set to infinity.

In[9]:= DeleteCases[dalist, 
 Alternatives @@ 
  Nearest[dalist, frameCenter, {Infinity, criticalRadius}]]

Out[9]= {{9, 6}, {6, 0}, {0, 5}, {10, 8}, {1, 2}, {10, 4}, {1, 1}, {7,
   7}, {6, 8}, {6, 10}, {7, 4}, {1, 8}, {10, 0}, {10, 7}, {6, 3}, {4, 
  0}, {9, 2}, {4, 7}, {1, 6}, {10, 8}, {7, 8}, {0, 10}, {3, 4}, {0, 
  0}, {8, 5}, {6, 0}, {2, 9}, {2, 4}, {8, 4}, {7, 4}, {3, 6}, {7, 
  10}, {1, 10}, {1, 4}, {8, 0}, {8, 9}, {2, 5}, {2, 9}, {3, 1}, {0, 
  6}, {10, 3}, {9, 6}, {8, 7}, {7, 6}, {7, 3}, {8, 9}}

--- edit ---

Regarding the complexity of DeleteCases with pattern-free Alternatives, if the input size is n and the set of alternatives has m elements, then it is O(n+m) rather than O(n*m). This as of version 8 of Mathematica.

The examples below will bear out this claim. We start with 10^5 elements and delete around 18000 of them. Takes .17 seconds. We then use 10x as many elements, and remove more than 10x as many (so n and m both go up by a factor of 10 or more). Total time is 1.6 seconds, or a factor of around 10 bigger.

In[90]:= dalist5 = RandomInteger[{-10, 10}, {10^5, 2}];
criticalRadius5 = 5;
Timing[rest5 = 
   DeleteCases[dalist5, 
    Alternatives @@ (closest5 = 
       Nearest[dalist5, {0, 0}, {Infinity, criticalRadius5}])];]
Length[closest5]

Out[92]= {0.17, Null}

Out[93]= 18443

In[94]:= dalist6 = RandomInteger[{-10, 10}, {10^6, 2}];
criticalRadius6 = 6;
Timing[rest6 = 
   DeleteCases[dalist6, 
    Alternatives @@ (closest6 = 
       Nearest[dalist6, {0, 0}, {Infinity, criticalRadius6}])];]
Length[closest6]

Out[96]= {1.61, Null}

Out[97]= 256465

-- end edit ---

Daniel Lichtlau

8
  • 3
    +1 for revealing this Nearest syntax. less-than-well might that mean not? Aug 26, 2011 at 16:23
  • 1
    @Sjoerd Not quite not. See tutorial/UsingNearest sixth usage syntax. I will not be surprised if this obscure bit of documentation migrates to the reference page for Nearest. Aug 26, 2011 at 18:54
  • 3
    @Leonid Correct idea but not for pattern matcher. Cases and DeleteCases were optimized for when the second argument is pattern free. They work in the manner you suggest though: (1) Hash the alternatives (2) Look up each element in the hash table. If there (or not, in DeleteCases) then Sow it. (3) (As in every good morality play:) Reap what you sowed. This was my contribution of Sept 9, 2009 (so I can say I did at least one useful thing that month). Aug 26, 2011 at 20:50
  • 2
  • 1
    @Daniel It seemed familiar - and, sure enough, I found myself on that thread (other branch: groups.google.com/group/comp.soft-sys.math.mathematica/…). I probably forgot about your comment there on the development kernel. I had two contributions there, and the more concise (and perhaps a faster one) is unsortedComplement[x_, y_] := x /. Dispatch[Thread[Union[y] -> Sequence[]]];. I can not test now, but from your numbers in that post, I expect a built-in for M8 to beat it 2-3 times. But that is not too bad for a top-level code (my version), I guess. Aug 26, 2011 at 21:33
1

Easy - Just use the center of the circle. From that (using Pythagoras theorem) calculate the distance from that centre. Compare that to the radius. Is less than or equal to the radius then it is inside the circle. Otherwise outside.

2
  • 1
    As an aside - You can use this method to compute PI. See the monte carlo method for pi (Google it is interested)
    – Ed Heal
    Aug 26, 2011 at 11:48
  • And the tag Mathematica is not math Aug 27, 2011 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.