簡體   English   中英

在二維數組的每一行中查找最大值的索引

[英]Find index of max value in each row of 2D array

我有以下代碼:

amplitude=[]
for i in range (0,160):
    amplitude.append(i+1)

#print(amplitude)

#split arrays up into a line for each sample
traceno=10                  #number of traces in file
samplesno=16             #number of samples in each trace. This wont change.

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

#print the maximum value of array along axis=1
max_amp=np.amax(amplitude_split,1)
print(max_amp)

#print the  indices of the maximum values along axis=1
ind_max_amp=np.argmax(amplitude_split, axis=1, out=None)
print(ind_max_amp)

我想在數組的每一行中找到最大值的索引。 我目前只得到ind_max_amp = [15 15 15 15 15 15 15 15 15 15]我認為這是每個最大值的列。

我需要的是元組:(0,15)(1,15)等......

另外,誰能幫我找到每行最大 90% 和 10% 的索引?

注意這只是測試代碼,我的真實數據的最大值不會都在最后一列

您可以通過np.amax使用axis=1keepdims獲取每行的最大值,以將每個最大值保留在單獨的行中,然后使用np.argwhere在每行中查找該最大值的索引。

這將為您提供所需的索引元組列表:

ind = np.argwhere(amplitude_split==np.amax(amplitude_split,1, keepdims=True))
list(map(tuple, ind))

output:

[(0, 15), (1, 15), (2, 15), (3, 15), (4, 15), (5, 15), (6, 15), (7, 15), (8, 15), (9, 15)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM