1

我有一个数组,它由 [一个元素编号,x 坐标,y 坐标,z 坐标,半径(极坐标),θ(极坐标)] 在这个数组中我需要找到最接近指定的两个值编号,一个在上面,一个在下面。这需要在包含 θ 值的数组的最后一列中找到。

值的范围是 0 - 1.5707 弧度(0 - 90 度),在我们的例子中,我们希望能够选择我们想要的指定数字的数量

number=9 
Anglestep = math.pi/2 / number 
Anglerange = np.arange(0,math.pi/2+anglestep,anglestep) #math.pi/2+anglestep so that we get math.pi/2 in   the array

例如,我需要找到指定值上方和下方的两个值:“0.17”

[...['4549', '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405],
 ['4535', '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374],
 ['4537', '5.6042995', '49.4534569', '0.0811241', 49.7699967073121, 0.11284330708918186],
 ['4538', '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648],
 ['4539', '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783],
 ['4540', '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019],
 ['4541', '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243],
 ['4542', '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624],
 ['4529', '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338],
 ['4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599]..]

所以我想要的输出在这种情况下是两个值:(0.16651831290560243,0.17975113416156624)

4

2 回答 2

1

@NPE 的答案对于一维数组是正确的,但您必须首先访问Angle数组的列。这取决于您的数组的dtype(数据类型)(您的数组似乎同时包含字符串和浮点数,这对于 numpy 数组是不允许的)。有两种方法可以解决它,一种是让它全部浮动,另一种是使用结构化 dtype:

所有花车

arr = np.array([
    ['4549',  '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405 ],
    ['4535',  '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374],
    ['4537',  '5.6042995', '49.4534569', '0.0811241', 49.7699967073121 , 0.11284330708918186],
    ['4538',  '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648],
    ['4539',  '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783],
    ['4540',  '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019 ],
    ['4541',  '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243],
    ['4542',  '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624],
    ['4529',  '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338],
    ['4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599 ]], dtype=float)

然后,要应用@Jaime 的方法,请使用

i = np.searchsorted(arr[:, -1], 0.17)
below = arr[i-1]
above = arr[i]

below
# array([  4.54100000e+03,   8.32986550e+00,   4.95605049e+01,   8.12513000e-02,   5.02556495e+01,   1.66518313e-01])
above
# array([  4.54200000e+03,   9.01412110e+00,   4.96065178e+01,   8.11457000e-02,   5.04188555e+01,   1.79751134e-01])

如果您只想要角度,那么也只需按列切片:

below_ang = arr[i-1, -1]
above_ang = arr[i, -1]

below_ang, above_ang
#(0.166518313, 0.179751134)

请注意,这假设arr按角度排序。

结构化数组:

arr = array([ ('4549',  '4.2158604', '49.4799309', '0.0833661', 49.65920902290997, 0.0849981532744405 ),
              ('4535',  '4.2867651', '49.4913025', '0.0813997', 49.67660795755971, 0.08640089283783374),
              ('4537',  '5.6042995', '49.4534569', '0.0811241', 49.7699967073121 , 0.11284330708918186),
              ('4538',  '6.2840257', '49.4676971', '0.0809942', 49.86523874780516, 0.12635612935285648),
              ('4539',  '6.9654546', '49.4909363', '0.0814121', 49.97869879894153, 0.13982362821749783),
              ('4540',  '7.6476088', '49.5210190', '0.0813955', 50.10805567128103, 0.1532211602749019 ),
              ('4541',  '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243),
              ('4542',  '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624),
              ('4529',  '9.3985014', '49.6320610', '0.0812080', 50.51409018950577, 0.18714756393388338),
              ('4531', '10.3884563', '49.7157669', '0.0812043', 50.78954127329902, 0.2059930152826599)], 
             dtype=[('id', 'S4'), ('x', 'S10'), ('y', 'S10'), ('z', 'S9'), ('rad', '<f8'), ('ang', '<f8')])

i = np.searchsorted(arr['ang'], 0.17)
below = arr[i-1]
above = arr[i]

below
# ('4541', '8.3298655', '49.5605049', '0.0812513', 50.25564948531672, 0.16651831290560243)
above
# ('4542', '9.0141211', '49.6065178', '0.0811457', 50.41885547537927, 0.17975113416156624)

为几个值做这件事

首先,设置范围的更简单方法是 with linspace,它自动包括开始和结束,并由数组的长度指定,而不是步长。代替:

number=9 
anglestep = math.pi/2 / number 
anglerange = np.arange(0,math.pi/2+anglestep,anglestep) #math.pi/2+anglestep so that we get math.pi/2 in the array

利用

number = 9
anglerange = np.linspace(0, math.pi/2, number) # start, end, number

现在,searchsorted实际上会很容易地为您找到几点:

locs = np.searchsorted(arr['ang'], anglerange)
belows = arr['ang'][locs-1]
aboves = arr['ang'][locs]

例如,我将设置,anglerange = [0.1, 0.17, 0.2]因为完整范围不在您的示例数据中:

belows
# array([ 0.08640089,  0.16651831,  0.18714756])
aboves
# array([ 0.11284331,  0.17975113,  0.20599302])
于 2013-11-14T15:33:48.973 回答
1
In [30]: np.max(arr[arr < .17])
Out[30]: 0.16651831290560243

In [31]: np.min(arr[arr > .17])
Out[31]: 0.17975113416156624
于 2013-11-14T10:21:05.877 回答