根据概率密度函数p(x,y,z)随机填充3D网格 [英] Randomly fill a 3D grid according to a probability density function p(x,y,z)
本文介绍了根据概率密度函数p(x,y,z)随机填充3D网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何按给定的概率密度函数指定的顺序填充3D网格?
我想使用python,以随机顺序放置点,但要根据该区域上的某些指定概率分布,而无需重复点.
Using python, I'd like to lay down points in a random order, but according to some specified probability distribution over that region, with no repeated points.
依次:
- 创建离散的3D网格
- 指定每个网格点pdf(x,y,z)的概率密度函数
- 放置一个点(x0,y0,z0),其随机位置与pdf(x,y,z)成正比
- 继续添加点(不重复),直到所有位置都填满为止
期望的结果是网格中所有点的所有点的列表(无重复),以便填充它们.
The desired result is a list of all points (no repeats) of all the points in the grid, in order that they were filled.
推荐答案
下面是使用高斯pdf的示例(请参见图).此代码很容易适应任何指定的pdf:
Here's an example, using a gaussian pdf (see plot). This code is easily adapted to any specified pdf:
%matplotlib qt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#number of points to lay down:
n = 4000;
#create meshgrid:
min, max, L = -5, 5, 91;
[x_grid,y_grid,z_grid] = np.meshgrid(np.linspace(min,max,L),np.linspace(min,max,L),np.linspace(min,max,L))
xi,yi,zi = x_grid.ravel(),y_grid.ravel(),z_grid.ravel()
#create normalized pdf (gaussian here):
pdf = np.exp(-(x_grid**2 + y_grid**2 + z_grid**2));
pdf = pdf/np.sum(pdf);
#obtain indices of randomly selected points, as specified by pdf:
randices = np.random.choice(np.arange(x_grid.ravel().shape[0]), n, replace = False,p = pdf.ravel())
#random positions:
x_rand = xi[randices]
y_rand = yi[randices]
z_rand = zi[randices]
fig = plt.figure();
ax = fig.add_subplot(111, projection='3d',aspect='equal')
svals = 16;
ax.scatter(x_rand, y_rand, z_rand, s=svals, alpha=.1)
这篇关于根据概率密度函数p(x,y,z)随机填充3D网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文