Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Select 在Mathematica中选择圆形区域内的点 请考虑: 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]}];_Select_Wolfram Mathematica_Conditional Statements - Fatal编程技术网

Select 在Mathematica中选择圆形区域内的点 请考虑: 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]}];

Select 在Mathematica中选择圆形区域内的点 请考虑: 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]}];,select,wolfram-mathematica,conditional-statements,Select,Wolfram Mathematica,Conditional Statements,我想创建一个测试来遍历dalist并拒绝位于frameCenter的某个半径内或某个半径上的点,如上图所示。我以前用矩形区域做过,但我不知道如何用圆形区域做,很简单-只要用圆心就行了。由此(使用毕达哥拉斯定理)计算出从该中心的距离。将其与半径进行比较。小于或等于半径,则位于圆内。除此之外。这将非常有效: In[82]:= Pick[dalist,UnitStep[criticalRadius^2-Total[(Transpose[dalist]-frameCenter)^2]],0] Out


我想创建一个测试来遍历dalist并拒绝位于frameCenter的某个半径内或某个半径上的点,如上图所示。我以前用矩形区域做过,但我不知道如何用圆形区域做,很简单-只要用圆心就行了。由此(使用毕达哥拉斯定理)计算出从该中心的距离。将其与半径进行比较。小于或等于半径,则位于圆内。除此之外。

这将非常有效:

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}}
或者

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}}

“最近点”对于查找给定点某个半径内的每个集合成员非常有用。一种是使用第三个参数的形式,这种形式的文件不多,它允许一对表示{max number,max distance}。在这种情况下,我们允许在“最大半径”(max radius)内容纳尽可能多的对象,因此“最大数”(max number)仅设置为无穷大

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}}
---编辑---

关于具有无模式替代的DeleteCases的复杂性,如果输入大小为n,并且替代集合具有m个元素,那么它是O(n+m)而不是O(n*m)。这是从Mathematica第8版开始的

下面的例子将证明这一说法。我们从10^5个元素开始,删除大约18000个元素。需要0.17秒。然后我们使用10倍多的元素,并移除10倍多的元素(因此n和m都会增加10倍或更多)。总时间是1.6秒,或者说是大约10秒大的一个因素

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
--结束编辑---


Daniel Lichtlau

作为旁白,您可以使用此方法计算PI。请参见pi的蒙特卡罗方法(谷歌对此感兴趣),标签
Mathematica
不是
math
+1,以显示这种最接近的语法。“不太好”这可能意味着不好吗?@Sjoerd不太好。请参阅教程/使用最接近的第六用法语法。如果这篇晦涩难懂的文档迁移到参考页以获得正确的想法,而不是模式匹配器,我不会感到惊讶。当第二个参数是无模式的时,Cases和DeleteCases进行了优化。但是,它们的工作方式是:(1)散列备选方案(2)查找散列表中的每个元素。如果有(或没有,在某些情况下),则播种。(3) (就像在每一个好的道德游戏中:)收获你所播种的。这是我在2009年9月9日的贡献(所以我可以说那个月我至少做了一件有用的事情)。@Leonid See:@Daniel它看起来很熟悉——而且,我发现自己也在这条线上(另一个分支:)。我可能忘了你对开发内核的评论。我在那里有两篇文章,更简洁(也许更快)的是
unsortedcompletion[x,y]:=x/。分派[Thread[Union[y]->Sequence[]]。我现在不能测试,但从你在那篇文章中的数字来看,我希望内置的M8能够击败它2-3倍。但对于顶级代码(我的版本)来说,这还不算太坏,我想,到目前为止所有的答案都可以。如果您觉得有必要测试点是否位于更复杂的多边形中,您可能希望查看以下mathgroup讨论: