1. <i id='8NvLJ'><tr id='8NvLJ'><dt id='8NvLJ'><q id='8NvLJ'><span id='8NvLJ'><b id='8NvLJ'><form id='8NvLJ'><ins id='8NvLJ'></ins><ul id='8NvLJ'></ul><sub id='8NvLJ'></sub></form><legend id='8NvLJ'></legend><bdo id='8NvLJ'><pre id='8NvLJ'><center id='8NvLJ'></center></pre></bdo></b><th id='8NvLJ'></th></span></q></dt></tr></i><div id='8NvLJ'><tfoot id='8NvLJ'></tfoot><dl id='8NvLJ'><fieldset id='8NvLJ'></fieldset></dl></div>
  2. <small id='8NvLJ'></small><noframes id='8NvLJ'>

    <legend id='8NvLJ'><style id='8NvLJ'><dir id='8NvLJ'><q id='8NvLJ'></q></dir></style></legend>
  3. <tfoot id='8NvLJ'></tfoot>

      <bdo id='8NvLJ'></bdo><ul id='8NvLJ'></ul>

      在numpy中索引3d网格数据的球形子集

      时间:2023-07-23

          • <bdo id='WU1qF'></bdo><ul id='WU1qF'></ul>

                <small id='WU1qF'></small><noframes id='WU1qF'>

              • <tfoot id='WU1qF'></tfoot>
                <i id='WU1qF'><tr id='WU1qF'><dt id='WU1qF'><q id='WU1qF'><span id='WU1qF'><b id='WU1qF'><form id='WU1qF'><ins id='WU1qF'></ins><ul id='WU1qF'></ul><sub id='WU1qF'></sub></form><legend id='WU1qF'></legend><bdo id='WU1qF'><pre id='WU1qF'><center id='WU1qF'></center></pre></bdo></b><th id='WU1qF'></th></span></q></dt></tr></i><div id='WU1qF'><tfoot id='WU1qF'></tfoot><dl id='WU1qF'><fieldset id='WU1qF'></fieldset></dl></div>
              • <legend id='WU1qF'><style id='WU1qF'><dir id='WU1qF'><q id='WU1qF'></q></dir></style></legend>
                  <tbody id='WU1qF'></tbody>
                本文介绍了在numpy中索引3d网格数据的球形子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个带坐标的 3d 网格

                I have a 3d grid with coordinates

                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 可以很大.我可以做一个简单的循环来找到我需要的东西

                and I need to index points (i.e. x[i],y[j],z[k]) within some radius R of a position (x0,y0,z0). N_i can be quite large. I can do a simple loop to find what I need

                points=[]
                i0,j0,k0 = floor( (x0,y0,z0)/grid_spacing )
                Nr = (i0,j0,k0)/grid_spacing + 2
                for 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,y0,k0)) < cutoff:
                                points.append((i,j,k))
                

                但这很慢.有没有更自然/更快的方法来使用 numpy 进行这种类型的操作?

                but this quite slow. Is there a more natural/ faster way to do this type of operation with numpy?

                推荐答案

                这个怎么样:

                import scipy.spatial as sp
                x = np.linspace(0, Lx, Nx)
                y = np.linspace(0, Ly, Ny)
                z = np.linspace(0, Lz, Nz)
                
                #Manipulate x,y,z here to obtain the dimensions you are looking for
                
                center=np.array([x0,y0,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 points
                X,Y,Z=np.meshgrid(x,y,z)
                data=np.vstack((X.ravel(),Y.ravel(),Z.ravel())).T
                
                distance=sp.distance.cdist(data,center.reshape(1,-1)).ravel()
                points_in_sphere=data[distance<cutoff]
                

                你应该可以做的不是最后两行:

                Instead of the last two lines you should be able to do:

                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]
                

                这篇关于在numpy中索引3d网格数据的球形子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用“matplotlib.mlab.griddata"时程序挂起 下一篇:为战舰创建和初始化 5x5 网格

                相关文章

                <tfoot id='tyDcc'></tfoot>
                  <i id='tyDcc'><tr id='tyDcc'><dt id='tyDcc'><q id='tyDcc'><span id='tyDcc'><b id='tyDcc'><form id='tyDcc'><ins id='tyDcc'></ins><ul id='tyDcc'></ul><sub id='tyDcc'></sub></form><legend id='tyDcc'></legend><bdo id='tyDcc'><pre id='tyDcc'><center id='tyDcc'></center></pre></bdo></b><th id='tyDcc'></th></span></q></dt></tr></i><div id='tyDcc'><tfoot id='tyDcc'></tfoot><dl id='tyDcc'><fieldset id='tyDcc'></fieldset></dl></div>
                  1. <small id='tyDcc'></small><noframes id='tyDcc'>

                    <legend id='tyDcc'><style id='tyDcc'><dir id='tyDcc'><q id='tyDcc'></q></dir></style></legend>

                      <bdo id='tyDcc'></bdo><ul id='tyDcc'></ul>