用MATLAB在空心球内生成随机点

时间:2017-04-14 19:24:48

标签: matlab math random probability

我有一个函数(从MATLAB文档中复制),它在球体内生成随机点。但是,球体内的某些点是不允许的。如何在允许半径为1<r<2的空心球体内生成随机点。换句话说,这些点应该位于1到2的半径上,因此r = [0 1]之间不允许有任何点。

编辑:完全忘记包含该功能

function [x,y,z] = PointsInSphere(r,n)

rvals = 2*rand(n,1)-1;
elevation = asin(rvals);
azimuth = 2*pi*rand(n,1);
radii = r*(rand(n,1).^(1/r));
[x,y,z] = sph2cart(azimuth,elevation,radii);
end

2 个答案:

答案 0 :(得分:1)

替换

  

2017-04-14 14:39:39,515 ERROR o.a.j.t.g.TestBeanGUI: This should never happen! java.lang.Error: null at org.apache.jmeter.testbeans.gui.TestBeanGUI.<init>(TestBeanGUI.java:159) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.GuiPackage.getGuiFromCache(GuiPackage.java:393) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.GuiPackage.getGui(GuiPackage.java:245) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.GuiPackage.getGui(GuiPackage.java:216) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.tree.JMeterTreeModel.addComponent(JMeterTreeModel.java:145) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:121) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:121) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:121) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:113) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.GuiPackage.addSubTree(GuiPackage.java:517) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.Load.insertLoadedTree(Load.java:194) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.Load.loadProjectFile(Load.java:131) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.Load.loadProjectFile(Load.java:102) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.LoadRecentProject.doActionAfterCheck(LoadRecentProject.java:67) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.AbstractActionWithNoRunningTest.doAction(AbstractActionWithNoRunningTest.java:45) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.ActionRouter.performAction(ActionRouter.java:74) ~[ApacheJMeter_core.jar:3.2 r1790748] at org.apache.jmeter.gui.action.ActionRouter.lambda$actionPerformed$0(ActionRouter.java:59) ~[ApacheJMeter_core.jar:3.2 r1790748] at java.awt.event.InvocationEvent.dispatch(Unknown Source) [?:1.8.0_111] at java.awt.EventQueue.dispatchEventImpl(Unknown Source) [?:1.8.0_111] at java.awt.EventQueue.access$500(Unknown Source) [?:1.8.0_111] at java.awt.EventQueue$3.run(Unknown Source) [?:1.8.0_111] at java.awt.EventQueue$3.run(Unknown Source) [?:1.8.0_111] at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_111] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) [?:1.8.0_111] at java.awt.EventQueue.dispatchEvent(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [?:1.8.0_111] at java.awt.EventDispatchThread.run(Unknown Source) [?:1.8.0_111]

通过

radii = r*(rand(n,1).^(1/r));

其中x = (s/r)^r; radii = r*(x+(1-x)*rand(n,1)).^(1/r); 是外半径,r是内半径(新函数参数)。

这取代了(s0) - 统一变量,该变量用于通过(1x)获得原始函数中的半径 - 统一变量。计算值1,使得获得的半径具有原始分布但截断到区间(xs)。这可确保在所需区域上生成的3D分布均匀

答案 1 :(得分:0)

您可以轻松生成0到1之间的2个随机数。 使用第一个作为半径(加1,使其在[1,2]中)。 将第二个随机数乘以2pi。 将其从极坐标转换为笛卡尔坐标,你应该完成。

编辑:使用第二个角度移动到3D。