x = linspace(0,Lx,Nx)y = linspace(0,Ly,Ny)z = linspace(0,Lz,Nz)
我需要在一个位置(x0,y0,z0)的某个半径R内索引点(即x [i],y [j],z [k]). N_i可能非常大.我可以做一个简单的循环来找到我需要的东西
points=[]i0,j0,k0 = floor( (x0,z0)/grID_spacing )Nr = (i0,k0)/grID_spacing + 2for i in range(i0-Nr,i0+Nr): for j in range(j0-Nr,j0+Nr): for k in range(k0-Nr,k0+Nr): if norm(array([i,j,k])*grID_spacing - (x0,k0)) < cutoff: points.append((i,k))
但这很慢.是否有一种更自然/更快捷的方式来进行numpy这种类型的 *** 作?
解决方法 这个怎么样:import scipy.spatial as spx = np.linspace(0,Nx)y = np.linspace(0,Ny)z = np.linspace(0,Nz)#Manipulate x,y,z here to obtain the dimensions you are looking forcenter=np.array([x0,z0])#First mask the obvIoUs points- may actually slow down your calculation depending.x=x[abs(x-x0)<cutoff]y=y[abs(y-y0)<cutoff]z=z[abs(z-z0)<cutoff]#Generate grID of pointsX,Y,Z=np.meshgrID(x,z)data=np.vstack((X.ravel(),Y.ravel(),Z.ravel())).Tdistance=sp.distance.cdist(data,center.reshape(1,-1)).ravel()points_in_sphere=data[distance<cutoff]
而不是最后两行你应该能够做到:
tree=sp.cKDTree(data)mask=tree.query_ball_point(center,cutoff)points_in_sphere=data[mask]
如果您不想调用空间:
distance=np.power(np.sum(np.power(data-center,2),axis=1),.5)points_in_sphere=data[distance<cutoff]总结
以上是内存溢出为你收集整理的python – 在numpy中索引3d网格数据的球形子集全部内容,希望文章能够帮你解决python – 在numpy中索引3d网格数据的球形子集所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)