在python数组中找到接近数字的值并将其索引

这是我到目前为止的代码:


import numpy as np


#make amplitude and sample arrays

amplitude=[0,1,2,3, 5.5, 6,5,2,2, 4, 2,3,1,6.5,5,7,1,2,2,3,8,4,9,2,3,4,8,4,9,3]

#print(amplitude)


#split arrays up into a line for each sample

traceno=5                  #number of traces in file

samplesno=6                #number of samples in each trace. This wont change.


amplitude_split=np.array(amplitude, dtype=np.int).reshape((traceno,samplesno))

print(amplitude_split)


#find max value of trace

max_amp=np.amax(amplitude_split,1)

print(max_amp)


#find index of max value

ind_max_amp=np.argmax(amplitude_split, axis=1, out=None)

#print(ind_max_amp)


#find 90% of max value of trace

amp_90=np.amax(amplitude_split,1)*0.9

print(amp_90)

我想在数组的每一行中找到最接近相应 amp_90 的值。我也希望能够获得这个数字的索引。请帮忙!


nb 我知道这很容易用肉眼完成,但在我将其应用于我的真实数据之前,它是一个测试数据集!


汪汪一只猫
浏览 138回答 1
1回答

人到中年有点甜

IIUC,您可以执行以下操作:# find the indices of the min absolute difference indices = np.argmin(np.abs(amplitude_split - amp_90[:, None]), axis=1)# get the values at those positionsresult = amplitude_split[np.arange(5), indices]print(result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python