[英]Sorting x,y,z coordinates into arrays within a defined volume Python/numpy
Hi I am fairly new and I hope you can answer my question or help me find a better method! 嗨,我相当新,我希望你能回答我的问题,或者帮我找到更好的方法!
Say I have a set of x,y,z coordinates that I want to subdivide into arrays containing the points within a certain volume (dV) of the total volume of the x,y,z space. 假设我有一组x,y,z坐标,我想细分为包含x,y,z空间总体积的特定体积(dV)内的点的数组。
I have been trying to sort each x,y,z coordinate by the x value first, then subdividing by some dx into a new dimension of the array, then within each of these subdivided dimensions, sorting the y values and redividing by dy, and then the same along the z axis, giving the sorted and subdivided coordinates 我一直试图先用x值对每个x,y,z坐标进行排序,然后将一些dx细分为数组的新维度,然后在每个细分维度内,对y值进行排序,并用dy重新划分,然后沿z轴相同,给出排序和细分的坐标
I have attempted to create an array to append the coordinate sets to... 我试图创建一个数组来附加坐标集...
def splitter(array1):
xSortx = np.zeros([10,1,3])
for j in range(0,10):
for i in range(len(array1)) :
if (j * dx) <= array1[i][0] < (j + 1)*dx:
np.append(xSortx[j],array1[i])
everything seemed to be working but the append part, i have heard append in python can be troubling so another method I tried was to create the multidimensional matrix first in order to fill it, but I ran into the problem that I do not know how to create a multidimensional matrix that could have for example 1 entry in the second dimension but 5 in the next index of the second ex: [[[0,0,0]],[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]]. 一切似乎都工作,但附加部分,我听说追加python可能是麻烦所以我尝试的另一种方法是先创建多维矩阵,以填补它,但我遇到了我不知道怎么样的问题创建一个多维矩阵,可以在第二维中有1个条目,但在第二个ex的下一个索引中有5个:[[[0,0,0]],[[0,0,0],[0,0 ,0],[0,0,0],[0,0,0],[0,0,0]]]。
I would really appreciate any tips or advice, let me know if this is not very clear and I will try to explain it more! 我真的很感激任何提示或建议,如果不是很清楚,请告诉我,我会尝试更多解释!
I believe this is what you want: 我相信这就是你想要的:
# define your working volume
Vmin = np.array([1,2,3])
Vmax = np.array([4,5,6])
DV = Vmax-Vmin
# define your subdividing unit
d = 0.5
N = np.ceil(DV / d).astype(int) # number of bins in each dimension
def splitter(array):
result = [[[[] for i in xrange(N[0])] for j in xrange(N[1])] for k in xrange(N[2])]
for p in array:
i,j,k = ((p - Vmin ) / d).astype(int) # find the bin coordinates
result[i][j][k].append(p)
return result
# test the function
test = Vmin + np.random.rand(20,3) * DV # create 20 random points in the working volume
result = splitter(test)
for i in xrange(N[0]):
for j in xrange(N[1]):
for k in xrange(N[2]):
print "points in bin:", Vmin + np.array([i,j,k]) * d
for p in result[i][j][k]:
print p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.