@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])